简体   繁体   中英

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

Need help for http.client library for doing a PUT request, like to know if there is a way to add header info and payload in the PUT request, I see documentation says as below, is there a way to embed header and payload info in the BODY? If so, could you please show an example.

import http.client

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

You can add header info as a dict on 4 arguments. As far as know is not possible embed in the 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"})

The command:

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

Is overloaded as below as well, so its pretty straight forward :)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM