POST request with form data using Python's request

22,379

You are doing it exactly right; sending a POST request with a text parameter gives me a 200 response with JSON body:

>>> import requests
>>> url = 'http://text-processing.com/api/phrases/'
>>> params = {'text': 'This is California.'}
>>> r = requests.post(url, data=params)
>>> r
<Response [200]>
>>> r.json()
{u'NP': [u'California', u'This'], u'GPE': [u'California'], u'VP': [u'is'], u'LOCATION': [u'California']}

If you get a 400 response instead you are sending too much data or an empty value:

Errors

A 400 Bad Request response will be returned under the following conditions:

  • no value for text is provided
  • text exceeds 1,000 characters
  • an incorrect language is specified

language can be english, dutch, portuguese or spanish, but defaults to english if you don't include it. If you do include it in your request you can also get a 400 error if you don't set it to one of those 4 supported values.

Share:
22,379
pg2455
Author by

pg2455

Updated on July 16, 2020

Comments

  • pg2455
    pg2455 almost 4 years

    I am trying to query this API from within Python. It seems to respond with 400 code. Can some one tell me how should this API be queried?

    This API is documented at http://text-processing.com/docs/phrases.html

    import requests
    r = requests.post('http://text-processing.com/api/phrases/',
                      data= {'text':'This is California.'})
    

    I guess I am misunderstanding the way that data should be posted here.