在Python 3中使用POST URI

I am trying to post data into a website that takes text data through the URL: "http://data.bioontology.org/annotator?text=" + theTextData

我想使用POST URI来获取数据。这是我的代码:

def get_annotations(text, url):
    headers = [('Authorization', 'apikey token=' + API_KEY)]
    data = text

    response = requests.request("POST",url,headers=headers,data=data)
    return response.text.encode('utf-8')

annotations = get_annotations(text, "http://data.bioontology.org/annotator?text=" + urllib.parse.quote(text))

我在运行它时收到此错误:

Traceback (most recent call last):
...
    response = requests.request("POST",url,headers=headers,data=data)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/requests/api.py", line 61, in request
    return session.request(method=method, url=url, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/requests/sessions.py", line 516, in request
    prep = self.prepare_request(req)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/requests/sessions.py", line 449, in prepare_request
    p.prepare(
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/requests/models.py", line 315, in prepare
    self.prepare_headers(headers)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/requests/models.py", line 447, in prepare_headers
    for header in headers.items():
AttributeError: 'list' object has no attribute 'items'

我该如何解决?我是否使用POST错误?

评论
  • 叛逆
    叛逆 回复

    您的标头应该是字典,而不是列表中的元组。

    headers = {'Authorization': 'apikey token=' + API_KEY}