I am trying to convert the following curl request to pycurl:
curl -v -H Accept:application/json \ -H Content-Type:application/json \ -d "{ name: 'abc', path: 'def', target: [ 'ghi' ] }" \ -X POST http:
I have python code:
import pycurl, json c = pycurl.Curl() c.setopt(pycurl.URL, 'http://some-url') c.setopt(pycurl.HTTPHEADER, ['Accept: application/json']) data = json.dumps({"name": "abc", "path": "def", "target": "ghi"}) c.setopt(pycurl.POST, 1) c.setopt(pycurl.POSTFIELDS, data) c.setopt(pycurl.VERBOSE, 1) c.perform() print curl_agent.getinfo(pycurl.RESPONSE_CODE) c.close()
While doing this, I got error 415: Unsupported media type, so I changed:
c.setopt(pycurl.HTTPHEADER, ['Accept: application/json'])
in
c.setopt(pycurl.HTTPHEADER, [ 'Content-Type: application/json' , 'Accept: application/json'])
This time I have a 400: Bad request. But bash code with curl works. Do you know what I should fix in python code?
source share