Getting The Data of a POST Request in Python - python

I am trying to get the skills returned after entering a Job title (such as "Process Analyst") on this website: https://match.emsiskills.com/skills/job.
After entering the job title, I observed that skills are called by a POST request. I tried matching the data required by this POST request but I keep getting a 415 error.
Here's my code:
import requests
skills_url = 'https://match.emsiskills.com/api/emsi-services/profiles/rankings/skills'
data = '{"filter":{"title":["15.74"]},"rank":{"by":"profiles","limit":60,"min_profiles":1}}'
r = requests.post(skills_url, data=data, json=True)
This returns a 415 error as mentioned earlier.
Any help is appreciated.

Thanks to #JustinEzequiel comment, it turns out I was using the calling post method incorrectly.
Here is the updated and working code:
import requests
skills_url = 'https://match.emsiskills.com/api/emsi-services/profiles/rankings/skills'
data = {"filter":{"title":["15.74"]},"rank":{"by":"profiles","limit":60,"min_profiles":1}}
r = requests.post(skills_url, json=data)

Related

How to get steamwebapi to return data

I'm trying to pull player stats for specific games. When I try to request data from the steam API, it throws an error. From what I have seen this URL format has worked for many others, I'm not sure why it throws errors.
import requests
key = ''
steamid = ''
r = requests.get(f'http://api.steampowered.com/ISteamUserStats/GetUserStatsForGame/v2/?appid=252490&key={key}&steamid={(steamid)}')
data = r.json()
print(data)
Returns: <Response [500]>
I noticed that you are using http.
Have you tried https?
You may need to pass the verify=False parameter in the request.get call.

Python fails to parse JSON in API calls

I want to write a python script that retrieves data from a website using it's API. Using the code below (which comes from this [tutorial][1] - click [here for more context][2]), I am able to successfully submit a query to the website but not retrieve the data I need.
import requests, sys, json
WEBSITE_API = "https://rest.uniprot.org/beta"
def get_url(url, **kwargs):
response = requests.get(url, **kwargs);
if not response.ok:
print(response.text)
response.raise_for_status()
sys.exit()
return(response)
r = get_url(f"{WEBSITE_API}/uniprotkb/search?query=parkin AND (taxonomy_id:9606)&fields=id,accession,length,ft_site", headers={"Accept": "text/tsv"})
print(r.text)
Instead of printing the data I've queried, I get an error message:
{"url":"http://rest.uniprot.org/uniprotkb/search","messages":["Invalid request received. Requested media type/format not accepted: 'text/tsv'."]}
I figured out that I can print the data as a JSON-formatted string if I change the second-to-last line of my code to this:
r = get_url(f"{WEBSITE_API}/uniprotkb/search?query=parkin AND (taxonomy_id:9606)&fields=id,accession,length,ft_site")
In other words, I simply remove the following code snippet:
headers={"Accept": "text/tsv"}
I highly doubt this is an issue with the website because the tutorial the non-working code comes from was from a November 2021 webinar put on by an affiliated institution. I think a more likely explanation is that my python environment lacks some library and would appreciate any advice about how to investigate this possibility. Another possible solution I would appreciate is code and/or library suggestions that would put me on the path to simply parsing the JSON-string into a tab-delimited file.
[1]: https://colab.research.google.com/drive/1SU3j4VmXHYrYxOlrdK_NCBhajXb0iGes#scrollTo=EI91gRfPYfbc
[2]: https://drive.google.com/file/d/1qZXLl19apibjszCXroC1Jg2BGjcychqX/view

Receiving 'BadRequestKeyError' when attempting to GET data from endpoint url

I was trying to implement an API using Python, and flask to help myself learn and practice REST.
The idea was to receive a HTTP POST with data that looks like as such:
{"startDate":"2015-07-01","endDate":2015-07-08","within":{"value":9000,"units":miles}} and send some of the data to a NASA API(endpoint).
I was able to create a POST method , and I am able to receive the data (both in POSTMAN and in the browser). Here is the relevant code :
#neows.route('/UserInput',methods=['GET','POST'])
def UserInput():
startDate = request.args.get('startDate')
endDate = request.args.get('endDate')
#print (type(startDate))
#print (type(endDate))
getAsteroids(startDate,endDate)
return jsonify(request.args)
But when I extract some data from the POST above to send to a NASA API (GET), I am receiving this error:
werkzeug.exceptions.BadRequestKeyError
Here is the url I am trying to hit : (https://api.nasa.gov/neo/rest/v1/feed?start_date=START_DATE&end_date=END_DATE&api_key=API_KEY)
I am able to hit the url both on POSTMAN and browser, outside of my code.
The relevant piece of code with the error is posted below and the line that seems to be throwing the error is in Italics (marked with *).
def getAsteroids(startDate,endDate):
API_KEY='xxx'
print (startDate)
print (endDate)
*result=request.args["https://api.nasa.gov/neo/rest/v1/feed?
start_date="+startDate+"&end_date="+endDate+"&api_key="+API_KEY+""]*
I would really appreciate if some one could help me understand and resolve this issue.
If you want to do a request against the NASA's API you can use requests module. (Or any other module to send HTTP requests)
import requests
# ...
def getAsteroids(startDate, endDate):
API_KEY='xxx'
payload = {'start_date': startDate, 'end_date': endDate, 'api_key': API_KEY}
result = requests.get('https://api.nasa.gov/neo/rest/v1/feed', params=payload)
request.args is something different used to get the parameters of the incoming request.

Zoopla property listing API returns "Unknown location" using python and requests

I am trying to retrieve the property listing using Python 3 and the requests library. For some reason, the API returns error code 7 "Unknown location entered". The same parameters seems to work well on Zoopla's IO-Docs where results are returned.
My request:
import requests
api_key = xxxxxxxxx
r3 = requests.get("https://api.zoopla.co.uk/api/v1/property_listings",data = {'area':'Oxford','api_key':api_key})'
The request returns the following:
print(r3.request.url) --> https://api.zoopla.co.uk/api/v1/property_listings
print(r3.request.body)' --> area=Oxford&api_key=xxxxxxxxx
print("Status code:",r3.status_code)' --> Status code: 400
print(r3.content)' -->b'<?xml version="1.0" encoding="UTF-8" standalone="yes" >\n<response>\n <error_code>7</error_code>\n <error_string>Unknown location entered.</error_string>\n</response>\n'
I don't understand why it returns "Unknown location" while the same parameters work fine on their website. Can anyone help, please?
Thank you.
When using the data parameter your data get submitted in the body of the request, as you have demonstrated in your question. Since this is a GET request, the body is propably ignored.
Instead, you should send your data in the query string with the params parameter.
import requests
api_key = 'xxxxxxxxx'
api_url = 'https://api.zoopla.co.uk/api/v1/property_listings.xml'
data = {'area':'Oxford','api_key':api_key}
r3 = requests.get(api_url, params=data)
print(r3.request.url)
print(r3.request.body)
'https://api.zoopla.co.uk/api/v1/property_listings.xml?area=Oxford&api_key=xxxxxxxxx'
None

Python 2 Zoho Post Error?

I am trying to add a lead to a Zoho CRM module with Python. I keep getting:
< response>< error>< code>4600< /code>< message>Unable to process your request. Please verify if the name and value is appropriate for the "xmlData" parameter.< /message>< /error>< /response>
from the server. I have no idea if I am posting correctly or if it is a problem with our Xml Data. I am using urllib and urllib2 to format the post request.
The post request looks like this.
url = ("https://crm.zoho.com/crm/private/xml/Leads/insertRecords?authtoken="
""+str(self.authToken)+"&scope=crmapi")
params = {"xmlData":self.xml}
data = urllib.urlencode(params)
request = urllib2.Request(url = url, data =data)
request.add_header("Content-Type",'application/xml')
response = urllib2.urlopen(request)
You cannot combine HTTP GET query parameters (ones in URL) and HTTP POST parameters.
This is limitation on the HTTP protocol level, not in Python or Zoho.
Most likely you are doing it wrong. Revisit Zoho documentation how it should be done.
Here is another old library doing Zoho + CRM, written in Python. You might want to check it for inspiration: https://github.com/miohtama/mfabrik.zoho

Categories