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:
Is this the correct way to send the resuest ?.
How should i specify the params value ?
Does content-type need to be specified ?
Please Help
Thank You
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 http://bugs.python.org/issue12524'
>>> conn.close()
Construct a dict of the data you want to be sent as a POST request.
urlencode the dict to get a string.
urlopen the URL you want, passing in the optional data parameter as your encoded POST data.
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)
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()
import requests
endpoint = 'https://xxxxxxxxxxxxxxxxxxx.com'
headers = {'Content-Type': 'text/plain'}
data = '{ id: 1 }'
result = requests.post(endpoint, headers=headers, data=data)
print(result)
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
Related
I am trying to access the github API via requests with python (Answers in similar questions here and here do not help).
Using curl, I am able to get a list of recent commits for a project, e.g.
curl -H "Accept: application/vnd.github.inertia-preview+json Authorization: token a0d42faabef23ab5b5462394373fc133ca107890" https://api.github.com/repos/rsapkf/42/commit
Trying to use the same setup in python with requests I tried to use
url = "https://api.github.com/repos/rsapkf/42/commits"
headers = {"Accept": "application/vnd.github.inertia-preview+json", "Authorization": "token a0d42faabef23ab5b5462394373fc133ca107890"}
r = requests.get(url, headers=headers)
as well as
url = "https://api.github.com/repos/rsapkf/42/commits"
headers = {"Accept": "application/vnd.github.inertia-preview+json"}
my_username = "someuser"
my_token = "a0d42faabef23ab5b5462394373fc133ca107890"
r = requests.get(url, headers=headers, auth=(my_username, my_token))
but in both cases I get the response
{'documentation_url': 'https://docs.github.com/rest',
'message': 'Bad credentials'}
What am I missing here?
No Authentication is required. The following works:
url = "https://api.github.com/repos/rsapkf/42/commits"
headers = {"Accept": "application/vnd.github.inertia-preview+json"}
r = requests.get(url, headers=headers)
Im trying to post data to my Django server but when I check the server the Querydict is empty. When I call from the browser it does look good.
import requests
import json
headers = {
"AUTHORIZATION": "1234",
"Content-type": "application/json",
"Accept": "text/plain"
}
print headers
payload = {
"start_date_s":"04/01/2016",
"start_date_e":"04/15/2016"
}
r = requests.post('http://localhost:8000/get-ticket-data',headers=headers, json = json.dumps(payload))
print r.content
print r.text
If you use the json argument, you should pass your dict as is, don't dump it (docs):
r = requests.post('http://localhost:8000/get-ticket-data',headers=headers, json=payload)
if you send json request, you need to use request.body in django view to get the that, not request.POST
I am trying to use post request in https website.But urllib work only on http.so can you tell me how to use urllib for https.
thanks in advance.
It's simply not true that urllib only works on HTTP, not HTTPS. It fully supports HTTPS.
In any case though, you probably want to be using the third-party library requests.
I'd rather use httplib:
import httplib
import urllib
params = urllib.urlencode({'user': 'pew', 'age': 52})
headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}
conn = httplib.HTTPSConnection("your_domain.com")
conn.request("POST", "/form/handler/test", params, headers)
response = conn.getresponse()
print response.status
print response.reason
reply = response.read()
print reply
I'm trying to use requests in python to post a json dictionary to a url. I need to get a string back from the url but I keep getting a code 141 error -{"code":141,"error":"Missing a github repository link"}. I'm using this website(http://docs.python-requests.org/en/latest/user/quickstart/) to do requests.
Any ideas on why I keep getting that error? Code is below.
import requests
import json
payload = { "email" : "jade#gmail.com", "github" : "https://github.com/"}
headers = {'content-type': 'application/json', "Accept": 'application/json'}
r = requests.post("http://challenge.code2040.org/api/register", params = payload, headers = headers)
print(r.url)
print r.text
Update: The suggestion worked but now I'm getting an{"code":141,"error":"success/error was not called"} error when I try to save the response I recieve from the url into a variable and then post it back to a different url.
#Store the token into a variable
token = r.text
payload = { "token" : token}
headers = {'content-type': 'application/json', "Accept": 'application/json'}
r = requests.post("http://challenge.code2040.org/api/getstring", json = payload, headers = headers)
print r.text
Since you are making a POST request and you need to provide a JSON in the request body, use json argument, not params:
r = requests.post("http://challenge.code2040.org/api/register",
json=payload,
headers=headers)
(tested - got back a token)
Note that json argument was introduced in requests 2.4.2.
This is the raw request for an API call:
POST http://192.168.3.45:8080/api/v2/event/log?sessionKey=b299d17b896417a7b18f46544d40adb734240cc2&format=json HTTP/1.1
Accept-Encoding: gzip,deflate
Content-Type: application/json
Content-Length: 86
Host: 192.168.3.45:8080
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.1.1 (java 1.5)
{"eventType":"AAS_PORTAL_START","data":{"uid":"hfe3hf45huf33545","aid":"1","vid":"1"}}
This request returns a success (2xx) response.
Now I am trying to post this request using requests:
import requests
headers = {'content-type' : 'application/json'}
data ={"eventType" : "AAS_PORTAL_START",
"data" : {"uid": "hfe3hf45huf33545",
"aid": "1",
"vid": "1"}
}
url = ("http://192.168.3.45:8080/api/v2/event/log?"
"sessionKey=9ebbd0b25760557393a43064a92bae539d962103&"
"format=xml&"
"platformId=1")
requests.post(url, params=data, headers=headers)
The response from this request is
<Response [400]>
Everything looks fine to me and I am not quite sure what I posting wrong to get a 400 response.
params is for GET-style URL parameters, data is for POST-style body information. It is perfectly legal to provide both types of information in a request, and your request does so too, but you encoded the URL parameters into the URL already.
Your raw post contains JSON data though. requests can handle JSON encoding for you, and it'll set the correct Content-Type header too; all you need to do is pass in the Python object to be encoded as JSON into the json keyword argument.
You could split out the URL parameters as well:
params = {'sessionKey': '9ebbd0b25760557393a43064a92bae539d962103', 'format': 'xml', 'platformId': 1}
then post your data with:
import requests
url = 'http://192.168.3.45:8080/api/v2/event/log'
data = {"eventType": "AAS_PORTAL_START", "data": {"uid": "hfe3hf45huf33545", "aid": "1", "vid": "1"}}
params = {'sessionKey': '9ebbd0b25760557393a43064a92bae539d962103', 'format': 'xml', 'platformId': 1}
requests.post(url, params=params, json=data)
The json keyword is new in requests version 2.4.2; if you still have to use an older version, encode the JSON manually using the json module and post the encoded result as the data key; you will have to explicitly set the Content-Type header in that case:
import requests
import json
headers = {'content-type': 'application/json'}
url = 'http://192.168.3.45:8080/api/v2/event/log'
data = {"eventType": "AAS_PORTAL_START", "data": {"uid": "hfe3hf45huf33545", "aid": "1", "vid": "1"}}
params = {'sessionKey': '9ebbd0b25760557393a43064a92bae539d962103', 'format': 'xml', 'platformId': 1}
requests.post(url, params=params, data=json.dumps(data), headers=headers)
Assign the response to a value and test the attributes of it. These should tell you something useful.
response = requests.post(url,params=data,headers=headers)
response.status_code
response.text
status_code should just reconfirm the code you were given before, of course
Set data to this:
data ={"eventType":"AAS_PORTAL_START","data":{"uid":"hfe3hf45huf33545","aid":"1","vid":"1"}}