4

I am unable to successfully convert and execute a curl post command to python code.

curl command

curl -X POST -H "Content-Type:application/json; charset=UTF-8" -d '{"name":joe, "type":22, "summary":"Test"}' http://url

Converted code

import requests
import json 

url="http://url"
data = {"name":joe, "type":22, "summary":"Test"}
headers = {'Content-type': "application/json; charset=utf8"}
response  = requests.post (url, data=json.dumps(data), headers=headers)
print response.text
print response.headers

I get no response in return, when I execute it manually from the shell it works fine, but when I execute the code, nothing happens, I don't see errors or anything.

Le Ray
  • 367
  • 2
  • 12
  • Yes, I just added it in the question. Nothing comes back, its empty – Le Ray Sep 26 '16 at 15:33
  • After modifying your code to get it to run. I successfully got a response on the following URL. http://httpbin.org/post I suggest you modify your example code so it is as short as is possible while still presenting this undesired behaviour. Also, make sure that it runs without us needing to do anything, (add the imports, remove joe variable) This might also help you see where the problem is not in. – TZubiri Sep 26 '16 at 15:45
  • ok, thank you for the advice. Were you able to locate the issue? – Le Ray Sep 26 '16 at 15:51
  • No, because I wasn't able to reproduce it. – TZubiri Sep 26 '16 at 15:53
  • Try this code, it is the simplest post request possible [mcve] that works: import requests url="http://httpbin.org/post"/ response = requests.post (url, "hi") print(response.text) – TZubiri Sep 26 '16 at 15:54
  • Can't test this second, but `data` expects a dictionary I think. Try changing `data=json.dumps(data)` to `data=data` – roganjosh Sep 26 '16 at 15:59
  • I was just able to fix it. My summary field in data was something like this "summary":"this is title". I moved the text out and assigned it to a variable and it worked. "summary":var. Not sure why it didnt work earlier... – Le Ray Sep 26 '16 at 16:04
  • Also, your dictionary looks malformed for `headers` with the semi-colon. Change to `headers = {'content-type': 'application/json', 'charset': 'utf8'}` – roganjosh Sep 26 '16 at 16:04

2 Answers2

1

If you are using one of the latest versions of requests: Try using the 'json' kwarg (no need to convert to json explicitly) instead of the 'data' kwarg:

response = requests.post(url, json=data, headers=headers)

Note: Also, this way you can omit the 'Content-type' header.

suselrd
  • 734
  • 4
  • 10
1

In case you have a proxy configured at your environment, define it at your session/request as well.

For example with session:

my_proxies = {  
'http': 'http://myproxy:8080',  
'https': 'https://myproxy:8080'  
}  

session = requests.Session()  
request = requests.Request('POST', 'http://my.domain.com', data=params_template, headers=req_headers, proxies=my_proxies)  
prepped = session.prepare_request(request)  
response = session.send(prepped)  

see documentation:
request http://docs.python-requests.org/en/master/user/quickstart/
session http://docs.python-requests.org/en/master/user/advanced/

Paul Roub
  • 36,322
  • 27
  • 84
  • 93
IsaacE
  • 305
  • 2
  • 10