postman 如何发送POST请求

anauzrmj  于 2022-11-23  发布在  Postman
关注(0)|答案(1)|浏览(270)

我如何接受POST请求?我通过Postman发送请求本身,或者可能我没有正确理解,我走错了地方,但任务的本质是这样的。我写了一个端点(POST),则需要向该端点发出POST请求,并Map数据、如果你能告诉我去哪里做正确的Map,我也会很感激。Map完成后,您需要将此数据发送到另一个端点但我仍然无法解决接受发布请求上的数据的问题。我该如何做?
正如我所说的,我有两个端点,第一个是,我将通过POSTMAN -〉path('api/v1/lead',CreateLeadAPI.as_view())发出POST请求。当我在POSTMAN中发出POST请求时,我希望Python可以获取JSON信息,进行数据Map,并将Map的数据发送到另一个端点

class CreateLeadAPI(generics.ListCreateAPIView):
    queryset = LeadId.objects.all()
    serializer_class = APISerializer
rdrgkggo

rdrgkggo1#

您可以使用不同的框架,如Django,Flask,Fast API。
但你需要一个简单的应用程序,你可以使用http.server:

import argparse
from http.server import HTTPServer, BaseHTTPRequestHandler

class S(BaseHTTPRequestHandler):
    def _set_headers(self):
        self.send_response(200)
        self.send_header("Content-type", "text/html")
        self.end_headers()

    def _html(self, message):
        """This just generates an HTML document that includes `message`
        in the body. Override, or re-write this do do more interesting stuff.
        """
        content = f"<html><body><h1>{message}</h1></body></html>"
        return content.encode("utf8")  # NOTE: must return a bytes object!

    def do_GET(self):
        self._set_headers()
        self.wfile.write(self._html("hi!"))

    def do_HEAD(self):
        self._set_headers()

    def do_POST(self):
        # Doesn't do anything with posted data
        self._set_headers()
        self.wfile.write(self._html("POST!"))

def run(server_class=HTTPServer, handler_class=S, addr="localhost", port=8000):
    server_address = (addr, port)
    httpd = server_class(server_address, handler_class)

    print(f"Starting httpd server on {addr}:{port}")
    httpd.serve_forever()

if __name__ == "__main__":

    parser = argparse.ArgumentParser(description="Run a simple HTTP server")
    parser.add_argument(
        "-l",
        "--listen",
        default="localhost",
        help="Specify the IP address on which the server listens",
    )
    parser.add_argument(
        "-p",
        "--port",
        type=int,
        default=8000,
        help="Specify the port on which the server listens",
    )
    args = parser.parse_args()
    run(addr=args.listen, port=args.port)

相关问题