【问题标题】:How do I set the content-type for POST requests in python-requests library?如何在 python-requests 库中设置 POST 请求的内容类型?
【发布时间】:2015-09-18 11:51:32
【问题描述】:

我当前使用的一个 API 指定我需要一个特殊的内容类型字符串。我不知道如何在 python-requests 库中设置它

【问题讨论】:

  • Post JSON using Python Requests 的可能重复项
  • 只设置标题?仅当您使用 datafilesjson 作为 requests.post() 调用的参数时,requests 才会设置 Content-Type,并且您始终可以使用 headers 字典覆盖它。

标签: python python-requests


【解决方案1】:
import requests

headers = {'Content-type': 'content_type_value'}
r = requests.get(url, headers=headers)

【讨论】:

  • 对于因谷歌搜索错误消息而最终来到这里的任何其他人:请注意,通常,您不想明确设置标题。 requests 库将为您完成。见stackoverflow.com/a/57479286/4629950。 Vikas Ohja 的回答仍然是正确的,因为在这种情况下,问题明确是关于手动设置。
猜你喜欢
  • 2013-09-09
  • 2017-12-25
  • 2018-05-26
  • 2011-07-24
  • 2018-12-18
  • 2012-04-07
  • 1970-01-01
  • 2018-02-09
  • 2015-08-18
相关资源
最近更新 更多