Joe
Joe

Reputation: 31

Sending POST request to a webservice from python

I'm trying to send a POST request to a restful webservice. I need to pass some json in the request.It works with the curl command below

curl --basic -i --data '<json data string here>' -H Content-type:"text/plain" -X POST http://www.test.com/api

I need some help in making the above request from Python. To send this POST request from python I have the following code so far:

import urllib
url='http://www.test.com/api'
params = urllib.urlencode... #What should be here ?
data = urllib.urlopen(url, params).read()

I have the following three questions:

  1. Is this the correct way to send the resuest ?.
  2. How should i specify the params value ?
  3. Does content-type need to be specified ?

Please Help Thank You

Upvotes: 3

Views: 15848

Answers (6)

David
David

Reputation: 4083

import requests

endpoint = 'https://xxxxxxxxxxxxxxxxxxx.com'
headers = {'Content-Type': 'text/plain'}
data = '{ id: 1 }'

result = requests.post(endpoint, headers=headers, data=data)
print(result)

Upvotes: 0

George
George

Reputation: 6084

Here's a sample snippet on POST request of json. The results will be printed in your terminal.

import urllib, urllib2

url = 'http://www.test.com/api'
values = dict(data=json.dumps({"jsonkey2": "jsonvalue2", "jsonkey2": "jsonvalue2"}))
data = urllib.urlencode(values)
req = urllib2.Request(url, data)
rsp = urllib2.urlopen(req)
content = rsp.read()

print content

Upvotes: -1

jfs
jfs

Reputation: 414149

You don't need urllib.urlencode() if Content-Type is not application/x-www-form-urlencoded:

import json, urllib2

data = {"some": "json", "d": ["a", "ta"]}
req = urllib2.Request("http://www.test.com/api", data=json.dumps(data),
                      headers={"Content-Type": "application/json"})
print urllib2.urlopen(req).read()

Upvotes: 1

vireshas
vireshas

Reputation: 816

the question deals with sending the parameters as "json".. you need to set the Content-Type to application/json in the headers and then send the paramters without urlencoding..

ex:

url = "someUrl"

data = { "data":"ur data"}


header = {"Content-Type":"application/json","User-Agent":"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)"}

#lets use httplib2

import httplib2 
http = httplib2.Http()

response, send = http.request(url,"POST",headers=header,body=data)

Upvotes: 1

Michael Mior
Michael Mior

Reputation: 28753

The documentation for httplib has an example of sending a post request.

>>> import httplib, urllib
>>> params = urllib.urlencode({'@number': 12524, '@type': 'issue', '@action': 'show'})
>>> headers = {"Content-type": "application/x-www-form-urlencoded",
...            "Accept": "text/plain"}
>>> conn = httplib.HTTPConnection("bugs.python.org")
>>> conn.request("POST", "", params, headers)
>>> response = conn.getresponse()
>>> print response.status, response.reason
302 Found
>>> data = response.read()
>>> data
'Redirecting to <a href="http://bugs.python.org/issue12524">http://bugs.python.org/issue12524</a>'
>>> conn.close()

Upvotes: 2

Katriel
Katriel

Reputation: 123622

  1. Construct a dict of the data you want to be sent as a POST request.
  2. urlencode the dict to get a string.
  3. urlopen the URL you want, passing in the optional data parameter as your encoded POST data.

Upvotes: 1

Related Questions