Post request and encoding

Hernán De Angelis variablestarlight at gmail.com
Mon Nov 2 12:21:15 EST 2020


Just reply to myself and whoever might find this useful.

encode() must be done within the request call:

header = {'Content-type':'application/xml', 'charset':'UTF-8'}
response = requests.post(server, data=request.encode('utf-8'), 
headers=header)

not in a previous separate line as I did.

Now it works. This wasn't an obvious way to proceed for me.

/H.


On 2020-11-02 10:06, Hernán De Angelis wrote:
> Hi everyone,
>
> I am writing a program that sends a post request to a server. The post 
> request may include keywords with Swedish characters (åöä).
>
> I noticed that requests that include strings without those characters 
> return a useful expected response. On the other hand, posts including 
> those characters return an 'empty' response. However, if I save that 
> same request to a file and send it using wget I do get a useful 
> response. This suggests to me that the problem should be in how the 
> post is sent or processed before being sent and not in how it is 
> generated.
>
> My request has the form:
>
> header = {'Content-type':'application/xml', 'charset':'utf-8'}
> response = requests.post(server, data=request, headers=header)
>
> I have tried of course adding
>
> request.encode('utf-8')
>
> before sending the request but it has not lead to a different result.
>
> I have spent several hours trying to understand whats going on here. I 
> thought at first that this had to do with an issue described here: 
> https://github.com/psf/requests/issues/5560 but apparently that issue 
> was closed as it was deemed to be unrelated to the requests library.
>
>
> Has anyone experienced this before? Is there something obvious I am 
> missing here?
>
> I am not new to programming but relatively new to Python, so bear with 
> me please.
>
>
> Thanks in advance
>
> /H.
>
>
> If it is of any use, I post the following too:
>
> python3 -m requests.help
> {
> "chardet": {
> "version": "3.0.4"
> },
> "cryptography": {
> "version": "3.1.1"
> },
> "idna": {
> "version": "2.10"
> },
> "implementation": {
> "name": "CPython",
> "version": "3.8.5"
> },
> "platform": {
> "release": "5.9.1-1-default",
> "system": "Linux"
> },
> "pyOpenSSL": {
> "openssl_version": "1010108f",
> "version": "19.1.0"
> },
> "requests": {
> "version": "2.24.0"
> },
> "system_ssl": {
> "version": "1010108f"
> },
> "urllib3": {
> "version": "1.25.10"
> },
> "using_pyopenssl": true
> }
>


More information about the Python-list mailing list