Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call an API using Python Requests library

I can't figure out how to call this api correctly using python urllib or requests.

Let me give you the code I have now:

import requests
url = "http://api.cortical.io:80/rest/expressions/similar_terms?retina_name=en_associative&start_index=0&max_results=1&sparsity=1.0&get_fingerprint=false"
params = {"positions":[0,6,7,29]}
headers = { "api-key" : key,
            "Content-Type" : "application/json"}
# Make a get request with the parameters.
response = requests.get(url, params=params, headers=headers)

# Print the content of the response
print(response.content)

I've even added in the rest of the parameters to the params variable:

url = 'http://api.cortical.io:80/rest/expressions/similar_terms?'
params = {
    "retina_name":"en_associative",
    "start_index":0,
    "max_results":1,
    "sparsity":1.0,
    "get_fingerprint":False,
    "positions":[0,6,7,29]}

I get this message back:

An internal server error has been logged @ Sun Apr 01 00:03:02 UTC 2018

So I'm not sure what I'm doing wrong. You can test out their api here, but even with testing I can't figure it out. If I go out to http://api.cortical.io/, click on the Expression tab, click on the POST /expressions/similar_terms option then paste {"positions":[0,6,7,29]} in the body textbox and hit the button, it'll give you a valid response, so nothing is wrong with their API.

I don't know what I'm doing wrong. can you help me?

like image 806
Legit Stack Avatar asked Dec 11 '22 07:12

Legit Stack


2 Answers

The problem is that you're mixing query string parameters and post data in your params dictionary. Instead, you should use the params parameter for your query string data, and the json parameter (since the content type is json) for your post body data.

When using the json parameter, the Content-Type header is set to 'application/json' by default. Also, when the response is json you can use the .json() method to get a dictionary.

An example,

import requests

url = 'http://api.cortical.io:80/rest/expressions/similar_terms?'
params = {
    "retina_name":"en_associative",
    "start_index":0,
    "max_results":1,
    "sparsity":1.0,
    "get_fingerprint":False
}
data = {"positions":[0,6,7,29]}
r = requests.post(url, params=params, json=data)

print(r.status_code)
print(r.json())
200
[{'term': 'headphones', 'df': 8.991197733061748e-05, 'score': 4.0, 'pos_types': ['NOUN'], 'fingerprint': {'positions': []}}]
like image 70
t.m.adam Avatar answered Dec 31 '22 16:12

t.m.adam


So, I can't speak to why there's a server error in a third-party API, but I followed your suggestion to try using the API UI directly, and noticed you're using a totally different endpoint than the one you're trying to call in your code. In your code you GET from http://api.cortical.io:80/rest/expressions/similar_terms but in the UI you POST to http://api.cortical.io/rest/expressions/similar_terms/bulk. It's apples and oranges.

Calling the endpoint you mention in the UI call works for me, using the following variation on your code, which requires using requests.post, and as was also pointed out by t.m. adam, the json parameter for the payload, which also needs to be wrapped in a list:

import requests
url = "http://api.cortical.io/rest/expressions/similar_terms/bulk?retina_name=en_associative&start_index=0&max_results=1&sparsity=1.0&get_fingerprint=false"
params = [{"positions":[0,6,7,29]}]
headers = { "api-key" : key,
            "Content-Type" : "application/json"}
# Make a get request with the parameters.
response = requests.post(url, json=params, headers=headers)

# Print the content of the response
print(response.content)

Gives:

b'[[{"term":"headphones","df":8.991197733061748E-5,"score":4.0,"pos_types":["NOUN"],"fingerprint":{"positions":[]}}]]'
like image 32
Matt Morgan Avatar answered Dec 31 '22 16:12

Matt Morgan