1

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)
Mohammad Yusuf
  • 16,554
  • 10
  • 50
  • 78
Sandeep Shyam
  • 123
  • 3
  • 10

2 Answers2

3

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"})
  • Thanks rouland for your answer, i was getting 500 internal error due to my mistake in payload content, thanks for your answer that conn.request is overloaded with extram params for payload and headers – Sandeep Shyam Jan 05 '17 at 12:04
2

The command:

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

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

conn.request("PUT", "url", payload, headers)
Yaron
  • 10,166
  • 9
  • 45
  • 65
Sandeep Shyam
  • 123
  • 3
  • 10