如何在 python http.client 方法中添加 header 和负载信息

how to add header and payload info in python http.client methods

需要 http.client 库的帮助来执行 PUT 请求,想知道是否有办法在 PUT 请求中添加 header 信息和负载, 我看到文档如下所述,有没有办法在 BODY 中嵌入 header 和有效负载信息?如果可以,请举个例子。

import http.client

BODY = "***filecontents***"
conn = http.client.HTTPConnection("localhost", 8080)
conn.request("PUT", "/file", BODY)

您可以添加 header 信息作为 4 个参数的字典。据了解不可能嵌入 BODY.

import http.client
BODY = "***filecontents***"
conn = http.client.HTTPConnection("127.0.0.1", 5000)
conn.connect()
conn.request("PUT", "/file", BODY, {"someheadername":"someheadervalues",                  
"someotherheadername":"someotherheadervalues"})

命令:

conn.request("PUT", "/file", BODY) 

也如下所示重载,所以它非常简单:)

conn.request("PUT", "url", payload, headers)