Via the tutorial at https://github.com/simplegeo/python-oauth2, I can create a signed Request object. But I don't understand how to send the request and receive anything back.
When I check the URL that I get from request.to_url(), I get a response. I just don't know how to get it programmatically.
To make a GET request, you can just do
import urllib2
response = urllib2.urlopen(request.to_url())
response_body = response.read() # in case you need it
For POST, you should be able to do
import urllib2
urllib2_req = urllib2.Request(request.url, request.to_postdata())
response = urllib2.urlopen(urllib2_req)
response_body = response.read() # in case you need it
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.
I am able to perform a web request and get back the response, using urllib.
from urllib import request
from urllib.parse import urlencode
response = request.urlopen(req, data=login_data)
content = response.read()
I get back something like b'{"token":"abcabcabc","error":null}'
How will i be able to parse the token information?
You can use the json module to load the binary string data and then access the token property:
token = json.loads(bin_data)['token']
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})
As in this post, I attempt to get the final redirect of a webpage as:
import urllib.request
response = urllib.request.urlopen(url)
response.geturl()
But this doesn't work as I get the "HTTPError: HTTP Error 300: Multiple Choices" error when attempting to use urlopen.
See documentation for these methods here.
EDIT:
This problem is different than the Python: urllib2.HTTPError: HTTP Error 300: Multiple Choices question, because they skip the error-causing pages, while I have to obtain the final destination.
As suggested by #abccd, I used the requests library. So I will describe the solution.
import requests
url_base = 'something' # You need this because the redirect URL is relative.
url = url_base + 'somethingelse'
response = requests.get(url)
# Check if the request returned with the 300 error code.
if response.status_code == 300:
redirect_url = url_base + response.headers['Location'] # Get new URL.
response = requests.get(redirect_url) # Make a new request.
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