Python Forum
Handle parameters in POST request for python webserver?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Handle parameters in POST request for python webserver?
#1
I have a python webserver:

from http.server import HTTPServer, BaseHTTPRequestHandler

from io import BytesIO


class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):

    def do_GET(self):
        self.send_response(200)
        self.end_headers()
        self.wfile.write(b'Hello, world!')

    def do_POST(self):
        content_length = int(self.headers['Content-Length'])
        body = self.rfile.read(content_length)
        self.send_response(200)
        self.end_headers()
        response = BytesIO()
        response.write(b'This is a POST request \n')
        response.write(b'Received: ')
        response.write(body)
        self.wfile.write(response.getvalue())

httpd = HTTPServer(('localhost', 8080), SimpleHTTPRequestHandler)
httpd.serve_forever()
Made from various snippets of code I've pieced together from forums. I want a simple webserver that accepts GET and POST requests.

Let's say I have a POST request:
localhost:8080/?paramone=alpha&paramtwo=bravo&paramthree=charlie

And I want to return the value of 'paramtwo' two the user, how do I go about doing this?
Reply
#2
Since the parameter paramtwo is shown in your url, this is definitely GET-request (or may be you have this parameter is sent as POST too behind the scene). To get get-parameters you need to parse self.path, e.g. something like this (not tested):

import urllib
print(urllib.parse.parse_qs(self.path))
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Python request (post/get) Drunknmonkie 1 2,627 Jan-19-2023, 02:02 PM
Last Post: prvncpa
  Post request not saving in DRF Dexty 0 894 Jun-03-2022, 12:35 PM
Last Post: Dexty
Brick how to work with 2 or more related tables in a Post request ikurorox 0 1,383 Dec-05-2021, 01:01 AM
Last Post: ikurorox
  Show HTML in Python application and handle click SamHobbs 2 2,663 Sep-28-2021, 06:27 PM
Last Post: SamHobbs
  POST request with form data issue web scraping hoff1022 1 2,649 Aug-14-2020, 10:25 AM
Last Post: kashcode
  The correct POST request abhie_lp 5 2,931 Jun-05-2020, 07:27 AM
Last Post: buran
  combining flask webserver and another applicaiton maciejMatthias 3 2,229 May-10-2020, 11:19 PM
Last Post: snippsat
  How to apply post request in python for establishment search for EPFO? rajeev1729 0 1,857 May-01-2020, 04:45 PM
Last Post: rajeev1729
  submit element from a list into a post request Godzilla 0 3,925 Mar-24-2018, 02:35 PM
Last Post: Godzilla
  I need request POST for Facebook in My Profile Kalet 4 4,246 Sep-27-2017, 05:53 PM
Last Post: Kalet

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020