I'm currently posting to a sever like so:
req = urllib2.Request('http://xxx.xxx.xx.xx/upload/')
req.add_header('Content-Type', 'application/json')
response = urllib2.urlopen(req, json_string)
print(response.getcode())
I get a 200 code back however I want to read the JSON the server is sending back. How do I do this? (tying to avoid using the requests library)
I did not get code, because I did not have a url.
Try:
req = urllib2.Request('http://xxx.xxx.xx.xx/upload/')
req.add_header('Content-Type', 'application/json')
response = urllib2.urlopen(req, json_string)
print(response.read())
To get the actual json object from the response not just the json serialised string you need to parse the response with the json library
import json
req = urllib2.Request('http://xxx.xxx.xx.xx/upload/')
req.add_header('Content-Type', 'application/json')
response = urllib2.urlopen(req, json_string)
json_response = json.loads(response.read().decode('ascii'))
The encoding may also be utf-8 depending on what the server sends back yo you.
Alternatively you could use the requests library which I find much easier to interact with, you'll need to install it separately though with pip install requests
import requests, json
response = requests.post('http://xxx.xxx.xx.xx/upload', data={'data': json_string})
if response.ok:
response_json = response.json()
else:
print('Something went wrong, server sent code {}'.format(response.status_code))
requests library docs
Related
I've been trying to use an api on a website for awhile now and the responses from the api return the data in xml. I want the response to be in JSON format, but when I try adding a header into the http request, it keeps sending the response in xml.
I've tried the following code:
import requests
param_list = {'key1': 'value1'}
headers = {'Content-Type': 'application/json'}
url = 'api url'
response = requests.get(url=url, params=param_list, headers=headers,)
print(response.text)
print(response.headers)
The second print statement shows that the 'Content-Type' header returns "text/html"
Any idea on how to fix this? Thanks for your time and help!
I have to get data from rest api using python. how to send headers to retrieve data from API. is there any module for requesting data from API.
Try requests it has two method get() and post()
Please try:
import requests
import json
res = requests.get('paste your link here')
response = json.loads(res.text)
Previous answers have covered the idea behind how to fetch data from an API using python. Requests library is a natural selection if you want to achieve this.
Documentation and ref: https://requests.readthedocs.io/en/master/
Installation: pip install requests or https://requests.readthedocs.io/en/master/user/install/#install
Coming to the last part - how to send headers to retrieve data from API?
You can pass headers as dictionary to the request.
url = 'https://api.github.com/some/endpoint'
headers = {'user-agent': 'my-app/0.0.1'}
response = requests.get(url, headers=headers)
Now you have the response object in response variable; now it's up to you what you want to achieve. e.g. If you want to see what is the response body as String;
print(response.text)
Yes python has requests lib to make a call to POST and GET methods
e.g.
import requests
url = 'web address'
params = {'key':'value'}
r = requests.get(url = url, params = params)
response = r.json()
I am struggling from 2 days with a post request to be made only using urllib & urllib2. I have limitations in using curl or requests library, as the machine I would need to deploy my code doesn't support any of these.
The post call would be accompanied with a Header and json Body. I am able to make any get call, but POST with Data & Header throws 400 bad requests. Tried and applied all the options available in google/stackoverflow, but nothing solved!
Below is the sample code:--
import urllib
import urllib2
url = 'https://1.2.3.4/rest/v1/path'
headers = {'X-Auth-Token': '123456789sksksksk111',
'Content-Type': 'application/json'}
body = {'Action': 'myaction',
'PressType': 'Format1', 'Target': '/abc/def'}
data = urllib.urlencode(body)
request = urllib2.Request(url, data, headers)
response = urllib2.urlopen(request, data)
And on setting debug handler, below is the format of the request that can be traced:--
send: 'POST /rest/v1/path HTTP/1.1\r\nAccept-Encoding: identity\r\nContent-Length: 52\r\nHost: 1.2.3.4\r\nUser-Agent: Python-urllib/2.7\r\nConnection: close\r\nX-Auth-Token: 123456789sksksksk111\r\nContent-Type: application/json\r\n\r\nAction=myaction&PressType=Format1&Target=%2Fabc%2Fdef'
reply: 'HTTP/1.1 400 Bad Request\r\n'
Please note, the same post request works perfectly fine with any REST client and with Requests library. In the debug handler output, if we see, the json structure is Content-Type: application/json\r\n\r\nAction=myaction&PressType=Format1&Target=%2Fabc%2Fdef, can that be a problem!
You can dump the json instead of encoding it. I was facing the same and got solved with it!
Remove data = urllib.urlencode(body) and use urllib2.urlopen(req, json.dumps(data))
That should solve.
I want to develop a python client for Pocket (formerly read it later).
I am studying the OAuth process of it. And be stuck here. How can I perform this request and get the response?
POST /v3/oauth/request HTTP/1.1
Host: getpocket.com
Content-Type: application/json; charset=UTF-8
X-Accept: application/json
{"consumer_key":"1234-abcd1234abcd1234abcd1234",
"redirect_uri":"pocketapp1234:authorizationFinished"}
I am new to python. This is I have tried. But I can not get the response I want.
#!/usr/bin/env python
import urllib2
import json
def main():
# Whatever structure you need to send goes here:
jdata = json.dumps({"consumer_key":"1234-abcd1234abcd1234abcd1234", "redirect_uri":"pocketapp1234:authorizationFinished"})
response = urllib2.urlopen("http://getpocket.com", jdata)
the_page = response.read()
print the_page
if __name__ == '__main__':
main()
Use the requests library for this sort of work. (EDITED)
import requests
import json
data = {"consumer_key": "..."}
headers = {"content-type": "application/json"}
response = requests.post("http://getpocket.com", data=json.dumps(data), headers=headers)
response.json
I want to use python urllib2 to simulate a login action, I use Fiddler to catch the packets and got that the login action is just an ajax request and the username and password is sent as json data, but I have no idea how to use urllib2 to send json data, help...
For Python 3.x
Note the following
In Python 3.x the urllib and urllib2 modules have been combined. The module is named urllib. So, remember that urllib in Python 2.x and urllib in Python 3.x are DIFFERENT modules.
The POST data for urllib.request.Request in Python 3 does NOT accept a string (str) -- you have to pass a bytes object (or an iterable of bytes)
Example
pass json data with POST in Python 3.x
import urllib.request
import json
json_dict = { 'name': 'some name', 'value': 'some value' }
# convert json_dict to JSON
json_data = json.dumps(json_dict)
# convert str to bytes (ensure encoding is OK)
post_data = json_data.encode('utf-8')
# we should also say the JSON content type header
headers = {}
headers['Content-Type'] = 'application/json'
# now do the request for a url
req = urllib.request.Request(url, post_data, headers)
# send the request
res = urllib.request.urlopen(req)
# res is a file-like object
# ...
Finally note that you can ONLY send a POST request if you have SOME data to send.
If you want to do an HTTP POST without sending any data, you should send an empty dict as data.
data_dict = {}
post_data = json.dumps(data_dict).encode()
req = urllib.request.Request(url, post_data)
res = urllib.request.urlopen(req)
import urllib2
import json
# Whatever structure you need to send goes here:
jdata = json.dumps({"username":"...", "password":"..."})
urllib2.urlopen("http://www.example.com/", jdata)
This assumes you're using HTTP POST to send a simple json object with username and password.
You can specify data upon request:
import urllib
import urllib2
url = 'http://example.com/login'
values = YOUR_CREDENTIALS_JSON
data = urllib.urlencode(values)
req = urllib2.Request(url, data)
response = urllib2.urlopen(req)
the_page = response.read()
You can use the 'requests' python library to achieve this:
http://docs.python-requests.org/en/latest/index.html
You will find this example:
http://docs.python-requests.org/en/latest/user/quickstart/#more-complicated-post-requests (More complicated POST requests)
>>> import requests
>>> payload = {'key1': 'value1', 'key2': 'value2'}
>>> r = requests.post("http://httpbin.org/post", data=payload)
It seems python do not set good headers when you are trying to send JSON instead of urlencoded data.