How to access data sent to my server using BaseHTTPRequestHandler?

I'm new to Python (using v3.3) and web programming, and I've been struggling with the problem all night. I send a POST call to my server and send it some data as follows:

DATA = {"listName":"Test list","listDesc":"A test list with test stuff in it.","refreshMode":"Replace","DBKey":"1","UserDisplaySeq":"1"} DATA = json.dumps(DATA) METHOD = "POST" DATA = DATA.encode("utf-8") params = "account_id=acct 2" try: URL = "http://localhost:8080/lists?" + quote_plus(params) request = urllib.request.Request(url=URL,data=DATA,method=METHOD) response = urllib.request.urlopen(request) ... 

I also have a request handler encoded as follows (there are many print statements here for debugging purposes):

 class MyHandler(BaseHTTPRequestHandler): ... def do_POST(self): length = int(self.headers['Content-Length']) print("HEADERS: ", self.headers) print (str(length)) print(self.rfile) post_data = urllib.parse.parse_qs(self.rfile.read(length).decode('utf-8')) print(post_data) 

This prints out the following result to the console:

 Starting thread started httpserver... HEADERS: Accept-Encoding: identity User-Agent: Python-urllib/3.3 Content-Length: 138 Content-Type: application/x-www-form-urlencoded Host: localhost:8080 Connection: close 138 <_io.BufferedReader name=404> {} 

My questions:
1) On the server (do_POST), how do I access the data that I think I am sending with my request (ie {"ListName": "List of tests", "listDesc": "Test ...)?

2) Is my request even sending data in the first place?

3) Is there a place where this is documented in a beginner's reach?

+6
source share
2 answers

Give it a try. I stole it from the answer to another question

 def do_POST(self): ctype, pdict = cgi.parse_header(self.headers.getheader('content-type')) if ctype == 'multipart/form-data': postvars = cgi.parse_multipart(self.rfile, pdict) elif ctype == 'application/x-www-form-urlencoded': length = int(self.headers.getheader('content-length')) postvars = cgi.parse_qs(self.rfile.read(length), keep_blank_values=1) else: postvars = {} print(postvars.get("listName", "didn't find it")) 
+5
source

1) On the server (do_POST), how can I access the data that I think I am sending with my request (for example, {"listName": "List of tests", "listDesc": "Test ...)

you can access the data simply by:

print self.rfile.read (length).

to make sure this works. you can do other parses. I suggest using simplejson to decode a json string. urllib.parse.parse_qs seems unnecessary.

2) Is my request even sending data in the first place?

 the code looks fine. to make sure it works, just try: curl -d "asdf" http://yourhost:yourport to see if the server have same response. so you can know whether the server side or client side goes wrong. 

3) Is there a place where this is documented in a beginner's reach?

 the official document is always a good choice: http://docs.python.org/2/library/basehttpserver.html 
+4
source

Source: https://habr.com/ru/post/949622/


All Articles