Python Session request is parsing the data argument wrongly - python

I'm trying to send a request to a server and also a json as data argument. If I use straight request, it works, but when I use session I get a bad request as result.
THIS WORKS:
import request
url = "https://whatever/api/v1/dosomething"
data = {"client":{"id":100},"job":{"name":"software developer","field_id":[1,2,3],"level":20}}
headers = {'Content-type': 'application/json'}
r = requests.get(url, data=json.dumps(data), headers=headers)
When I check r.request.body, I get the following:
'{"client": {"id": 100}, "job": {"name": "software developer", "field_id": [1, 2, 3], "level": 20}}'
THIS DOES NOT WORK:
When I try to use Session though, the request.body gets messed up.
url = "https://whatever/api/v1/dosomething"
data = {"client":{"id":100},"job":{"name":"software developer","field_id":[1,2,3],"level":20}}
headers = {'Content-type': 'application/json'}
s = requests.Session()
r = s.get(url, headers=headers, data=data, verify=False)
I get a bad request as a result of the code above, and when I check r.request.body:
client=id&job=name&job=field_id&job=level
I think I'm getting a bad request because of the request.body that got parsed in a wrong way, but I am not finding how to parse it correctly.
I already tried to use:
req = Request('GET', url, data=data, headers=headers)
prepped = req.prepare()
resp = s.send(prepped)
Can someone please help?
Thanks

You also need to json.dumps(data) when sending json with session.
So it will be
r = s.get(url, headers=headers, data=json.dumps(data), verify=False)
Not
r = s.get(url, headers=headers, data=data, verify=False)

Related

How to create a simple REST api WEB application with Python

We can make REST api application with spring boot by start.spring.io web site easily, anyone know any good website through which I can get the skeleton REST api project with python? My intention is to make REST api application with python.
One of the ways to do this is by using the requests module in Python. Import it into your code with the following command:
import requests
Now, each API is different, so you'll have to see with the vendor what the requirements are. For testing and learning purposes, I recommend using httpbin (https://httpbin.org). You can test pretty much anything there.
Here are a few simple requests:
#returning status
url = 'https://httpbin.org/post'
response = requests.post(url)
print(response.status_code)
print(response.ok)
#sending data/getting text response
url = 'https://httpbin.org/post'
params = {'Jello':'World'}
response = requests.post(url, params=params)
print(response.text)
#sending data/getting json response
url = 'https://httpbin.org/post'
params = {'Jello':'World'}
response = requests.post(url, params=params)
print(response.json())
#sending time data to server
import datetime
url = 'https://httpbin.org/post'
params = {'Time':f'{datetime.datetime.now()}'}
response = requests.post(url, params=params)
print(response.text)
#Params vs Data
#params
url = 'https://httpbin.org/post'
params = {'username':'jsmith','password':'abc123'}
response = requests.post(url, params=params)
print(response.text)
#data
url = 'https://httpbin.org/post'
payload = {'username':'jsmith','password':'abc123'}
response = requests.post(url, data=payload)
print(response.text)
# ********* HEADERS **********
url = 'https://httpbin.org/get'
response = requests.get(url)
print(response.text)
url = 'https://httpbin.org/post'
headers = {'content-type': 'multipart/form-data'}
response = requests.post(url,headers=headers)
print(response.request.headers) #client request headers
print(response.headers) #server response headers
print(response.headers['content-type']) #request header value from server
To use actual APIs, I suggest RapidAPI (https://rapidapi.com/), which is a hub where you can connect to thousands of APIs. HEre is a sample code using Google translate:
#RapidAPI
#Google Translate
import requests
url = "https://google-translate1.p.rapidapi.com/language/translate/v2"
text = 'Ciao mondo!'
to_lang = 'en'
from_lang = 'it'
payload = f"q={text}&target={to_lang}&source={from_lang}"
headers = {
'content-type': "application/x-www-form-urlencoded",
'accept-encoding': "application/gzip",
'x-rapidapi-host': "google-translate1.p.rapidapi.com",
'x-rapidapi-key': "your-API-key"
}
response = requests.post(url, data=payload, headers=headers)
print(response.json()['data']['translations'][0]['translatedText'])

API responds with Bad Request Error message

Could someone take a look at this code and let me know if there's something I'm doing wrong:
import requests
url = "https://api-end-point"
payload = {"grant_type": "client_credentials", "client_id":"my_ic", "client_secret": "not_now_please"}
headers = '{"accept": "application/json", "content-type": "application/json"}'
response = requests.request("POST", url, data=payload, headers=headers)
print(response.text)
For some reason it's throwing error.
{"type":"http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html","title":"Bad Request","status":400,"detail":"JSON decoding error: Syntax error, malformed JSON"}
So first use json instead of data, second don't use ' around the headers, or actually you don't need to specify headers:
import requests
url = "https://api-end-point"
payload = {"grant_type": "client_credentials", "client_id":"my_ic", "client_secret": "not_now_please"}
response = requests.request("POST", url, json=payload)
print(response.text)
The page does not contain json/application but html/text. If you remove the headers then it works:
response = requests.request(method="POST", url=url, data=payload)
If you have special needs to specify in the headers add details to your question

how to pass a response object to requests in python?

import json
import requests
url = 'smeurl/execute'
payload ={"sme":"yes"}
headers = {'Accept': 'application/json','content-type': 'application/json'}
r = requests.post(url, data=json.dumps(payload), headers=headers)
responseObj =r.json()
print responseObj
url="myurl/get?key="+str(responseObj )+""
r = requests.get(url, headers=headers)
if r.status_code == 200:
print r.json()
I want to pass responseObj to requests.get() in python but when its run its showing as
ValueError: No JSON object could be decoded
inspite of
responseObj prints what is expected(ie key)
also this url when run in RESTClient with this key
its giving json datawhich is what to be brought to python code.
Any suggestions pls?

Python Requests Code 141 Error

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.

how to use Google Shortener API with Python

I want to write an app to shorten url. This is my code:
import urllib, urllib2
import json
def goo_shorten_url(url):
post_url = 'https://www.googleapis.com/urlshortener/v1/url'
postdata = urllib.urlencode({'longUrl':url})
headers = {'Content-Type':'application/json'}
req = urllib2.Request(
post_url,
postdata,
headers
)
ret = urllib2.urlopen(req).read()
return json.loads(ret)['id']
when I run the code to get a tiny url, it throws an exception: urllib2.HTTPError: HTTP Error 400: Bad Requests.
What is wrong with this code?
I tried your code and couldn't make it work either, so I wrote it with requests:
import requests
import json
def goo_shorten_url(url):
post_url = 'https://www.googleapis.com/urlshortener/v1/url'
payload = {'longUrl': url}
headers = {'content-type': 'application/json'}
r = requests.post(post_url, data=json.dumps(payload), headers=headers)
print(r.text)
Edit: code working with urllib:
def goo_shorten_url(url):
post_url = 'https://www.googleapis.com/urlshortener/v1/url'
postdata = {'longUrl':url}
headers = {'Content-Type':'application/json'}
req = urllib2.Request(
post_url,
json.dumps(postdata),
headers
)
ret = urllib2.urlopen(req).read()
print(ret)
return json.loads(ret)['id']
I know this question is old but it is high on Google.
Another thing to try is the pyshorteners library it is very simple to implement.
Here is a link:
https://pypi.python.org/pypi/pyshorteners
With an api key:
import requests
import json
def shorten_url(url):
post_url = 'https://www.googleapis.com/urlshortener/v1/url?key={}'.format(API_KEY)
payload = {'longUrl': url}
headers = {'content-type': 'application/json'}
r = requests.post(post_url, data=json.dumps(payload), headers=headers)
return r.json()

Categories

Resources