18

Here is the curl command:

curl -H "X-API-TOKEN: <API-TOKEN>" 'http://foo.com/foo/bar' --data # 

let me explain what goes into data

POST /foo/bar
Input (request JSON body)

Name    Type    
title   string  
body    string

So, based on this.. I figured:

curl -H "X-API-TOKEN: " 'http://foo.com/foo/bar' --data '{"title":"foobar","body": "This body has both "double" and 'single' quotes"}'

Unfortunately, I am not able to figure that out as well (like curl from cli) Though I would like to use python to send this request. How do i do this?

frazman
  • 32,081
  • 75
  • 184
  • 269
  • 1
    Have you tried a library called pycurl? It's literally an emulation of curl with the exact same settings and almost the same syntax. – Vedaad Shakib Feb 12 '15 at 01:39

1 Answers1

37

With the standard Python httplib and urllib libraries you can do

import httplib, urllib

headers = {'X-API-TOKEN': 'your_token_here'}
payload = "'title'='value1'&'name'='value2'"

conn = httplib.HTTPConnection("heise.de")
conn.request("POST", "", payload, headers)
response = conn.getresponse()

print response

or if you want to use the nice HTTP library called "Requests".

import requests

headers = {'X-API-TOKEN': 'your_token_here'}
payload = {'title': 'value1', 'name': 'value2'}

r = requests.post("http://foo.com/foo/bar", data=payload, headers=headers)
cangoektas
  • 679
  • 5
  • 7