I am trying to make a post request in python and I believe I am doing everything correct. However it is not returning any response. I can't seem to figure out if there is anything wrong with my request. It seems like there may be something wrong with the service if I am not getting any response back. Is there anything inherently wrong with what I've written here?
import json
import urllib2
data = {'first_name': 'John','last_name': 'Smith','email': 'johnsmith#smith.com','phone': '215-555-1212'}
req = urllib2.Request('https://someurl.io/')
req.add_header('Content-Type', 'application/json')
response = urllib2.urlopen(req, json.dumps(data))
print response.read()
print response.headers
Honestly, unless you love urllib2, I suggest using requests. Here's the same data code in requests:
import requests
import json
payload = {'first_name': 'John','last_name': 'Smith','email': 'johnsmith#smith.com','phone': '215-555-1212'}
url = 'https://someurl.io/'
r = requests.post(url, json=json.dumps(payload))
print r.content
print r.headers
Related
How do i make Json requests to an endpoint to show the response in Json rather than show the response
i sent a POST request to a server and i am getting this as response.
D:\python>python runVacc.py
<Response [200]>
Which means its a successful request, but i want it to show both the Success as well as the Json Response. My Code is looking thus :
import requests
import json
url = 'URL HERE'
data = {"email":"tim#john.com","is_permanent":True,"bvn":"12345678901","tx_ref":"VAL12","phonenumber":"08098688235","firstname":"John","lastname":"Fish","narration":"Test ACC"}
res = requests.post(url, data=json.dumps(data),headers={'Content-Type':'application/json','Authorization':'Bearer FLWSECK_TEST-2033696a107e162088cdb02f777fa44e-X'})
print(res)
How do I make it to print Json Response containing the Json Data?
Please help, I am new to this.
Use print(res.content) instead.
So i was following the guide for getting a OAUTH token from https://developer.spotify.com/documentation/general/guides/authorization/client-credentials/
I tried doing a python requests library of the above equivalent code but i got response 400 from the server. May I know where i am going wrong?
import requests
import json
import clientidandsecret
headers ={"Authorization": "Basic " + clientidandsecret.C_ID +":" +clientidandsecret.C_SECRET}
form = {"form":{"grant_type" :"client_credentials"}, "json":"true"}
result = requests.post("https://accounts.spotify.com/api/token", headers=headers, data=form)
print(result)
Your variable form is a Dict, if you want to use the parameter data in requests it needs to be a string, this will fix it:
import json
...
result = requests.post(url, headers=headers, data=json.dumps(form))
Or even better:
result = requests.post(url, headers=headers, json=form)
I need to send python requests data in application/x-www-form-urlencoded. Couldn;t find the answer. It must be that format otherwise the web won;t pass me :(
simple request should work
import requests
url = 'application/x-www-form-urlencoded&username=login&password=password'
r = requests.get(url)
or a JSON post:
import requests
r = requests.post('application/x-www-form-urlencoded', json={"username": "login","password": password})
I have been successfully implementing python Requests module to send out POST requests to server with specified
resp = requests.request("POST", url, proxies, data, headers, params, timeout)
However, for a certain reason, I now need to use python urllib2 module to query. For urllib2.urlopen's parameter "data," what I understand is that it helps to form the query string (which is the same as Requests "params"). requests.request's parameter "data," on the other hand, is used to fill the request body.
After searching and reading many posts, examples, and documentations, I still have not been able to figure out what is the corresponding parameter of requests.request's "data" in urllib2.
Any advice is much appreciated! Thanks.
-Janton
It doesn't matter what it is called - it is a matter of passing it in at the right place. For example in this example, the POST data is a dictionary (name can be anything).
The dictionary is urlencoded and the urlencoded name can again be anything but I've picked "postdata", which is the data that is POSTed
import urllib # for the urlencode
import urllib2
searchdict = {'q' : 'urllib2'}
url = 'https://duckduckgo.com/html'
postdata = urllib.urlencode(searchdict)
req = urllib2.Request(url, postdata)
response = urllib2.urlopen(req)
print response.read()
print response.getcode()
If your POST data is plain text (not a Python type such as a dictionary) it can work without urllib.urlencode:
import urllib2
searchstring = 'q=urllib2'
url = 'https://duckduckgo.com/html'
req = urllib2.Request(url, searchstring)
response = urllib2.urlopen(req)
print response.read()
print response.getcode()
I'm fairly new to Python and I'm trying to execute a HTTP Request to a URL that returns JSON. The code, I have is:
url = "http://myurl.com/"
req = urllib.request.Request(url)
response = urllib.request.urlopen(req)
data = response.read()
I'm getting an error reading: "'bytes' object has no attribute 'read'". I searched around, but haven't found a solution. Any suggestions?
You may find the requests library easier to use:
import requests
data = requests.get('http://example.com').text
or, if you need the raw, undecoded bytes,
import requests
data = requests.get('http://example.com').content