POST 使用 Python 的请求和表单数据请求
POST request with form data using Python's request
我正在尝试从 Python 中查询此 API。它似乎以 400 代码响应。有人能告诉我应该如何查询这个 API 吗?
此 API 记录在 http://text-processing.com/docs/phrases.html
import requests
r = requests.post('http://text-processing.com/api/phrases/',
data= {'text':'This is California.'})
我想我误解了应该在此处发布数据的方式。
你做的完全正确;发送一个带有 text
参数的 POST
请求给我一个带有 JSON body:
的 200 响应
>>> 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']}
如果您收到 400 响应,则表明您发送的数据过多或空值:
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
可以是 english
、dutch
、portuguese
或 spanish
,但如果不包含则默认为 english
它。如果您将它包含在您的请求中,如果您没有将它设置为这 4 个支持的值之一,您也会收到 400 错误。
我正在尝试从 Python 中查询此 API。它似乎以 400 代码响应。有人能告诉我应该如何查询这个 API 吗?
此 API 记录在 http://text-processing.com/docs/phrases.html
import requests
r = requests.post('http://text-processing.com/api/phrases/',
data= {'text':'This is California.'})
我想我误解了应该在此处发布数据的方式。
你做的完全正确;发送一个带有 text
参数的 POST
请求给我一个带有 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']}
如果您收到 400 响应,则表明您发送的数据过多或空值:
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
可以是 english
、dutch
、portuguese
或 spanish
,但如果不包含则默认为 english
它。如果您将它包含在您的请求中,如果您没有将它设置为这 4 个支持的值之一,您也会收到 400 错误。