Post request and encoding

Chris Angelico rosuav at gmail.com
Mon Nov 2 12:29:00 EST 2020


On Tue, Nov 3, 2020 at 4:22 AM Hernán De Angelis
<variablestarlight at gmail.com> wrote:
>
> 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.
>

I'm not sure of all the context here, but most likely, what you're
seeing is that your data needs to be bytes and you had text. When you
call the encode method, you don't change the data itself - what
happens is that it returns an encoded form of the data. That's why
just calling request.encode() won't do anything - it creates the
encoded form but then does nothing with it.

Hopefully that helps a bit.

ChrisA


More information about the Python-list mailing list