0

I'm sure this is really banal and stupid but I cannot figure it out for the life of me.

I'm working with the Alchemy API, where the POST request using Requests looks like

r = requests.post(url, payload, headers=headers)

where payload is a dict with a field for url-encoded text like text=this%20is%text

The stupid problem I'm having is that if I use urllib for the encoding like myText = urlencode({'text': textToEncode})

I can't then insert it into the payload where payload={'text': myText} because then I get something probably like text=text=this%20is%text

This would be so much simpler if I could just encode the string itself. The encoding function adds this stuff I can't turn around an insert into the dict because the whole dict gets encoded.

Am I missing something incredibly simple and dumb?

jww
  • 97,681
  • 90
  • 411
  • 885
AutomaticStatic
  • 1,661
  • 3
  • 21
  • 42

1 Answers1

1

Try including the Content-type header, for example application/json and then using data=json.dumps(payload).

import json
headers['Content-type'] = 'application/json'
r = requests.post(url, data=json.dumps(payload), headers=headers)
wim
  • 338,267
  • 99
  • 616
  • 750