I am trying to make a HTTP request to an API using the POST method. The API that I am using is meant to take in three parameters (key1, key2, key3) and return a json file. Unfortunately, my POST request does not seem to return anything when I am using the data method to pass my dictionary to the API. This seems to be very strange because it seems to work when I am using the params method. I cannot try to understand this as this process seems to be very opaque (e.g. I cannot a URL to see how the payload are passed onto the API).
My question: What am I doing wrong here?
POST request where the parameters are sent over to the API using data method:
import requests
url = 'http://example.com/API.php'
payload = {
'key1': '<<Contains very long json string>>',
'key2': 5,
'key3': 0
}
print len(str(payload)) # Prints 6717
r = requests.post(url, data=payload) << Note data is used here
print r.status_code # Prints 200
print r.text # Prints empty string
POST request code where the parameters are sent over to the API using the params method:
import requests
url = 'http://example.com/API.php'
payload = {
'key1': '<<Contains very long json string>>',
'key2': 5,
'key3': 0
}
print len(str(payload)) # Prints 6717
r = requests.post(url, params=payload) << Note here params is used here
print r.status_code # Prints 200
print r.text # Prints expected JSON results
If you are wondering why I would like to use the data method over params... I am trying to pass other dictionaries containing longer strings and the params method does not seem to do it because I am getting ERROR 414. I was hoping that the error could be resolved by using data.
The API that I am using was written in PHP.