Python not receiving json HTTP response correctly - python

I've been trying to use the GENIUS API with python, but i don't get a full response.
Code:
base_url = "http://api.genius.com"
headers = {'Authorization': 'Bearer ExYL8I-s_5jmbMPobq9WtNpywmmATJr5owM1X3DPIiGI_uYJJRrsKv6Y26KrxVCy'}
/* temporary key */
song_title = "Radioactive"
search_url = base_url + "/search"
data = {'q': song_title}
response = requests.get(search_url, data=data, headers=headers)
json_r = response.json()
The response that I'm getting is:
{'meta': {'status': 200}, 'response': {'hits': []}}
The json parser is not parsing what's in the [ ] ...(square brackets). I then tried it in javscript using jquery and ajax and it worked perferctly, and received all the json data.

Change http to https:
base_url = "https://api.genius.com"

Related

Getting content-type: text/html instead of application/JSON Yelp API

I am trying to get data from Yelp's API but as the title suggests I am not getting a JSON and I'm not sure what to do. Below is my code
import requests
api_key = 'MY API KEY'
# USING THE YELP BUSINESS SEARCH API: https://www.yelp.com/developers/documentation/v3/business_search
# HEADERS CONTAIN THE API KEY
headers = {'Authorization': 'Bearer {}'.format(api_key), 'content-type': 'application/json'}
search_api_url = 'https://www.yelp.com/developers/documentation/v3/business_search'
params = {
'term': 'restaurants',
'location': 'Cincinnati, Ohio',
'limit': 50
}
# MAKE THE GET CALL WITH A TIME OUT OF 5 SECONDS
response = requests.get(search_api_url, headers=headers, params=params, timeout=5)
print(response.url)
print(response.status_code)
print(response.headers)

Python put request. Spotify API put request Malformed Json

I would really appreciate some help here. I am trying to use the Spotify API to add albums to a users library. I have been struggling with a Malformed Json Payload, and am completely out of ideas.
Here is a simplified version of what I am currently sitting on
url = 'https://api.spotify.com/v1/me/albums'
payload = {'body': ['01kTgTBiZkCFY3ZH2hBH6u', '4sz6Fn4BYORRLIc1AvQwQx']}
headers = {'Authorization':'Bearer {}'.format(access_token), 'Content-Type':'application/json',}
response = requests.put(url,headers=headers, data=payload)
print(response.json())
The error I am receiving is in the json response:
{'error': {'status': 400, 'message': 'Malformed json payload'}}
I have tried changing requests.put as per below, but all attempts are returning the same error
response = requests.put(url,headers=headers, json=payload)
response = requests.put(url,headers=headers, data=json.dumps(payload))
response = requests.put(url,headers=headers, json=json.dumps(payload))
07bYtmE3bPsLB6ZbmmFi8d: This spotify id is for the album, Dancefloor Hits #1. I checked my spotify acct and there it was in my Albums on my acct. Below is my code to run that.
import requests
url = "https://api.spotify.com/v1/me/albums"
payload = {"ids": "27cZdqrQiKt3IT00338dws"}
headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer %s' % (access_token) # paste your access token in here
}
''' you can use the same syntax as above but the key was that your ID of album had to be a **parameter** not ***data***.
To do that you use the params kwarg'''
response = requests.request("PUT", url, headers=headers, params = payload)
print(response.status_code) # should print out 200 when you run the code
# shows whether status was valid

Connecting to a rest API with python. How to setup headers & parameters?

i'm working on settin up a rest api with python, however i'm having some problem getting it to work.
I'm working with the TV DB rest api: https://api.thetvdb.com/swagger
and using python with Requests library to pull out the information.
My code is currently:
import json
import requests
URL = "https://api.thetvdb.com/"
API_KEY = "Api_key"
USER_KEY = "Key"
USERNAME = "Name"
headers = {"Accept": "application/json"}
params = {
"apikey": API_KEY,
"userkey": USER_KEY,
"username": USERNAME
}
resp = requests.post(URL + "login/", headers = headers ,params=params)
if resp.status_code != 200:
print('error: ' + str(resp.status_code))
else:
print('Success')
So far i'm only getting error code 401, not sure why.
Solved:
2 Things needed to be changed
1. The resp was changed into:
resp = requests.post(URL + "login/", headers = headers, data=json.dumps(params))
The header had to have
"Content-Type": "application/json"
added to it :) It's now working, thanks everyone
The login parameters probably need to be a JSON-encoded string POSTed as the body of the message.
Try resp = requests.post(URL + "login/", headers = headers, data=json.dumps(params))

Add a member using python requests and Mailchimp API v3

I am trying to learn more about using http requests and the Mailchimp API, but I cannot seem to figure out how to add a member to list using a post request. I have tried multiple configurations, and I guess I made some headway since my response went from a 405, to a 401, now I'm getting a 400. I assume this means that I am being authenticated, but I am formatting the request incorrectly.
I have gotten it to work the python mailchimp library, but I want to actually learn how to use the HTTP requests. I could find very few examples of using python requests with Mailchimp.
(obviously I put in my actual list_id, my_username, and my_apikey)
import requests, json
members_url = 'https://us15.api.mailchimp.com/3.0/lists/XXXXXXXX/members/'
auth = ('my_username', 'my_apikey')
headers = {'Content-Type': 'application/json'}
data1 = {
'email_address':'blah#blah.com',
'status':'subscribed',
'merge_fields':{
'FNAME':'John',
'LNAME':'Doe'
}
}
payload = json.dumps(data1)
response = requests.post(members_url, auth=auth, headers=headers, json=payload)
This is my response:
>>> response
<Response [400]>
I'm stumped....what am I doing wrong?
This worked:
import requests, json
members_url = 'https://us15.api.mailchimp.com/3.0/lists/XXXXXXXX/members/'
auth = ('my_username', 'my_apikey')
headers = {'Content-Type': 'application/json'}
data1 = {
'email_address':'<a real email address>', #it could tell 'blah#blah.com was fake'
'status':'subscribed',
'merge_fields':{
'FNAME':'John',
'LNAME':'Doe'
}
}
response = requests.post(members_url, auth=auth, headers=headers, json=data1)
This worked:
import requests, json
members_url = 'https://us15.api.mailchimp.com/3.0/lists/XXXXXXXX/members/'
auth = ('my_username', 'my_apikey')
headers = {'Content-Type': 'application/json'}
data1 = {
'email_address':'<a real email address>', #it could tell 'blah#blah.com was fake'
'status':'subscribed',
'merge_fields':{
'FNAME':'John',
'LNAME':'Doe'
}
}
response = requests.post(members_url, auth=auth, headers=headers, json=data1)
My problems were #1 I was double encoding the json data and #2 mailchimp recognizes obviously BS email addresses like blah#blah.com
Thanks #Alasdair

Print response JSON from server with HTTP POST

How can I get respon JSON dictionaries from server with POST:
import json
import requests
url = 'http://apiurl.com'
parameters = {'code':1,
'user': 'username',
'password': 'password'
}
headers = {'content-type': 'application/json'}
response = requests.post(url, data = json.dumps(parameters),headers=headers)
print(response)
output: Response [200]
response = requests.post(url, data=json.dumps(parameters), headers=headers)
print(response)
print(response.text)
Since, you are going to receive a JSON object you can simply use request's built-in JSON decoder Simply do:
j = response.json()

Categories