Reputation: 135
How do I convert json to x-www-form-urlencoded in Python?
I am using the code below to convert, but it is different from the Postman-encoded values, due to which I am getting an error.
import json
from urllib.parse import urlencode
j = json.dump('json code')
encoded = urlencode(json.loads(j),encode='utf-8')
I am getting the error below in my POST request.
"error": {
"status": 500,
"message": "Unexpected token '",
"message_shortcode": "unexpected token '",
"datetime": "20180102131407",
"url": "http://127.0.0.1:3001/api/searches/0SH69023763",
"qs": "type=form&result_options=%7B%22fieldset%22%3A%5B%22count%22%5D%7D",
"type": "InternalServerError",
"group": "server"
}
Upvotes: 1
Views: 9011
Reputation: 68
It clearly looks like, the data your server API is expecting is not being passed in expected format which is breaking the server side code execution. Please try to check what are the exact API's requirements along with headers.
If it expects Content-Type header to be 'x-www-form-urlencoded', then make sure you pass it as well when you are making request to the API URL.
With requests module it can be easier for implementing your code without even using urllib's urlencode. So better you also look at here also: http://docs.python-requests.org/en/v0.10.7/user/quickstart/
Upvotes: 1