Error reading return content

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Post Reply
Rat_financial
Posts: 2
Joined: Sun Jul 18, 2021 3:31 pm

Error reading return content

Post by Rat_financial » Sun Jul 18, 2021 3:47 pm

currently using google app script
When using python post , it returns json normally but when using micropython post it returns html
code:

Code: Select all

import prequests as requests
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
upload = {"data": "88"}
res = requests.post(url, json = upload, headers = headers)
print(res.text)
Last edited by jimmo on Mon Jul 19, 2021 1:49 am, edited 1 time in total.
Reason: Add [code] formatting

marcidy
Posts: 133
Joined: Sat Dec 12, 2020 11:07 pm

Re: Error reading return content

Post by marcidy » Sun Jul 18, 2021 5:00 pm

It would be helpful if you posted what you received, and what the server "normally" sends back. You really should inspect the full response to see if it's the same with both python and micropython, to see if the problem is with processing the response or with the request itself.

Also I have no idea what prequests is or how it's written so can't really help with it. Where did you get it?

Using code tags (the </> button) around your code makes it much easier to read on the forum.

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Error reading return content

Post by jimmo » Mon Jul 19, 2021 1:52 am

I think this has come up before, and what we found is that the HTML returned is actually an error page. (And hopefully that will explain what's going on). Perhaps a missing header that Python is setting, but MicroPython isn't?

I also notice that you're telling the server that it's form-urlencoded data but sending JSON... Maybe Python's request fixes that for you.
See viewtopic.php?f=18&t=6492

Rat_financial
Posts: 2
Joined: Sun Jul 18, 2021 3:31 pm

Re: Error reading return content

Post by Rat_financial » Mon Jul 19, 2021 3:05 am

jimmo wrote:
Mon Jul 19, 2021 1:52 am
I think this has come up before, and what we found is that the HTML returned is actually an error page. (And hopefully that will explain what's going on). Perhaps a missing header that Python is setting, but MicroPython isn't?

I also notice that you're telling the server that it's form-urlencoded data but sending JSON... Maybe Python's request fixes that for you.
See viewtopic.php?f=18&t=6492
use python status_code = 200 , but use micropython status_code = 405
but The information is normal post
prequests.py form https://gist.github.com/SpotlightKid/86 ... 41dd269c44

marcidy
Posts: 133
Joined: Sat Dec 12, 2020 11:07 pm

Re: Error reading return content

Post by marcidy » Mon Jul 19, 2021 7:51 pm

402 implies the server doesn't like the request, and I think jimmo's point is worth looking into. Looking at (macro)python's requests , it does switch the content-type if the json parameter is not none.

from requests.models.py:

Code: Select all

        if not data and json is not None:                                       
            # urllib3 requires a bytes-like body. Python 2's json.dumps         
            # provides this natively, but Python 3 gives a Unicode string.         
            content_type = 'application/json'                                   
 
...

Code: Select all

	else:                                                                   
            # Multi-part file uploads.                                          
            if files:                                                           
                (body, content_type) = self._encode_files(files, data)          
            else:                                                               
                if data:                                                        
                    body = self._encode_params(data)                            
                    if isinstance(data, basestring) or hasattr(data, 'read'):   
                        content_type = None                                     
                    else:                                                       
                        content_type = 'application/x-www-form-urlencoded' 
so try switching content-type to `application/json`.

Post Reply