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

5.1k views Asked by At

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)
2

There are 2 answers

1
Quentin Rouland On BEST ANSWER

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"})
0
Sandeep Shyam On

The command:

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

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

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