【问题标题】:Convert C# requests to Python requests将 C# 请求转换为 Python 请求
【发布时间】:2021-06-30 13:53:48
【问题描述】:

我在 C# 中有以下代码:

request.AddParameter("application/json", "{\r\n \"content\": \"" + message + "\"\r\n}", ParameterType.RequestBody);

我想将该代码转换为 python。这是我到目前为止所得到的:

data = {
    'content': 'test'
}


r = requests.post(url, data=data)

我不明白 application/json 部分的位置。

【问题讨论】:

标签: python c# python-3.x


【解决方案1】:

application/json 是内容类型。您可以像这样指定请求的内容类型

headers = {
      'content-type': 'application/json'
    }

data = {
    'content': 'test'
}


r = requests.post(url, data=data, headers=headers)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-02
    • 2019-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-14
    • 2016-01-30
    • 2020-03-18
    相关资源
    最近更新 更多