【问题标题】:Python requests headers can I use a 2d dictionary?Python请求标头我可以使用二维字典吗?
【发布时间】:2021-11-06 06:32:45
【问题描述】:

我需要将以下内容格式化为请求库的标头,但我不知道该怎么做

Content-Type: application/x-www-form-urlencoded
Authorization:
        OAuth oauth_consumer_key="your_consumer_key",
        oauth_nonce="random_string_or_timestamp",
        oauth_signature="your_consumer_secret&",
        oauth_signature_method="PLAINTEXT",
        oauth_timestamp="current_timestamp",
        oauth_callback="your_callback"
User-Agent: some_user_agent

我想把它做成一个二维字典,但在请求中尝试这样做只会给我一个错误,说第二级应该是一个字符串而不是一个字典。

任何帮助将不胜感激。

【问题讨论】:

    标签: python rest https python-requests


    【解决方案1】:

    尝试将Authorization 标头放在一行。例如:

    headers = {
        "Content-Type": "application/x-www-form-urlencoded",
        "Authorization": 'OAuth oauth_consumer_key="your_consumer_key",oauth_nonce="random_string_or_timestamp",oauth_signature="your_consumer_secret&",oauth_signature_method="PLAINTEXT",oauth_timestamp="current_timestamp",oauth_callback="your_callback"',
        "User-Agent": "some_user_agent",
    }
    
    url = "https://httpbin.org/get"
    
    print(requests.get(url, headers=headers).text)
    

    【讨论】:

      猜你喜欢
      • 2011-03-17
      • 2011-09-01
      • 2023-01-05
      • 1970-01-01
      • 1970-01-01
      • 2012-12-18
      • 1970-01-01
      • 1970-01-01
      • 2014-08-07
      相关资源
      最近更新 更多