Posting a json data in url -- Python - python

I have JSON data stored in a variable in [{"totalspend": 3240.650785131, "dailybudget": 50.0}] format.
I am trying to post this JSON data to a url using:
import requests
r = requests.post("myurl", myjson)
but I am not able to see the result on my url after executing the code.

Your server most likely expects the Content-Type: application/json header to be set:
r = requests.post("myurl", data=myjson,
headers={'Content-Type': 'application/json'})
Do make sure that myjson is an actual JSON string and not a Python list.
If you are using requests version 2.4.2 or newer, you can leave the encoding of the JSON data entirely to the library; it'll set the correct Content-Type header for you automatically. You'd pass in the Python object (not a JSON string) to the json keyword argument:
r = requests.post("myurl", data=myobject)

You need to set headers first :
Try :
import json
import requests
payload = {"totalspend": 3240.650785131, "dailybudget": 50.0}
headers = {'content-type': 'application/json'}
r = requests.post(url, data=json.dumps(payload), headers=headers)

Related

Python, Requests HTTP 415 error [duplicate]

One API I'm currently using specifies that I need a special content-type string. I don't know how do I set this in python-requests library
import requests
headers = {'Content-type': 'content_type_value'}
r = requests.get(url, headers=headers)

Loading JSON using Python Requests wrong type

I'm trying to load some data to ML using Python. This works ok but the type is set to 'T' and not to 'J' within ML.
I'd like to resolve this. The header setting seems to be just for show so how do I do this?
# Sending data
data = {'meting': '477', 'bericht': '473', 'plant': '01'}
url = 'http://server:8000/v1/documents?database=thijsPlantjes&extension=json'
headers = {'Content-Type': 'application/json'}
r = requests.post(url, json = json.dumps(data), auth=HTTPDigestAuth('plantje', 'password'), headers = headers)
If you use json parameter, requests will serialize for you, so you don't need to json.dumps yourself.
And it will also set content-type for you; you can drop headers keyword argument.
r = requests.post(url, json=data, auth=HTTPDigestAuth('plantje', 'password'))
According to requests documentation:
Instead of encoding the dict yourself, you can also pass it directly
using the json parameter (added in version 2.4.2) and it will be
encoded automatically:

Django Rest Framework: empty request.data

I have the following code for a view of DRF:
from rest_framework import viewsets
class MyViewSet(viewsets.ViewSet):
def update(self, request, pk = None):
print pk
print request.data
I call the URL via python-requests in the following way:
import requests
payload = {"foo":"bar"}
headers = {'Content-type': 'application/json'}
r = requests.put("https://.../myPk", data= payload, headers=headers)
but when the request is received from the server, request.data is empty. Here there is the output:
myPk
<QueryDict: {}>
How can I fix this problem?
You need to send the payload as a serialized json object.
import json
import requests
payload = {"foo":"bar"}
headers = {'Content-type': 'application/json'}
r = requests.put("https://.../myPk/", data=json.dumps(payload), headers=headers)
Otherwise what happens is that DRF will actually complain about:
*** ParseError: JSON parse error - No JSON object could be decoded
You would see that error message by debugging the view (e.g. with pdb or ipdb) or printing the variable like this:
def update(self, request, pk = None):
print pk
print str(request.data)
Check 2 issues here:-
Json format is proper or not.
Url is correct or not(I was missing trailing backslash in my url because of which I was facing the issue)
Hope it helps
Assuming you're on a new enough version of requests you need to do:
import requests
payload = {"foo":"bar"}
r = requests.put("https://.../myPk", json=payload, headers=headers)
Then it will properly format the payload for you and provide the appropriate headers. Otherwise, you're sending application/x-www-urlformencoded data which DRF will not parse correctly since you tell it that you're sending JSON.

BLS API data using cURL and requests library?

All, I'm trying to implement a curl request to get data from the BLS. Following their example here (they show the curl request), my code looks like this:
import requests
headers = {'Content-type': 'application/json'}
params = {"seriesid":["LEU0254555900", "APU0000701111"],"startyear":"2002", "endyear":"2012"}
p = requests.post('http://api.bls.gov/publicAPI/v1/timeseries/data/', params = params,headers = headers)
print p.url
print p.content
I'm getting the following (error) output:
http://api.bls.gov/publicAPI/v1/timeseries/data/?seriesid=LEU0254555900&seriesid=APU0000701111&endyear=2012&startyear=2002
{"status":"REQUEST_FAILED","responseTime":0,"message":["Sorry, an
internal error occurred. Please check your input parameters and try
your request again."],"Results":null}
Anyone had to deal with the BLS api and python?
Is the requests library the best for this?
You need to send the data as json, not pass it as a params dict. params sets the url parameters, which is not what you want, you need to pass it as data.
This should work:
import requests
import json
headers = {'Content-type': 'application/json'}
data = json.dumps({"seriesid":["LEU0254555900", "APU0000701111"],"startyear":"2002", "endyear":"2012"})
p = requests.post('http://api.bls.gov/publicAPI/v1/timeseries/data/', data=data, headers=headers)
print p.url
print p.content

How to use python urllib2 to send json data for login

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.

Categories