7

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?

6
  • Have you checked whether it's a issue with Cortical API and not your code? Commented Apr 1, 2018 at 0:20
  • yes I've checked because you can go to their website and test their api, I talked about how to do that in 2nd to last paragraph. their api is up. Commented Apr 1, 2018 at 0:27
  • I took your url and copied and pasted it in my browser. It's giving me the same internal server error message. Could you please check the url too? Commented Apr 1, 2018 at 0:29
  • @troymyname00 well you don't have my api key, but you can go to their webiste api.cortical.io and try the api there. Commented Apr 1, 2018 at 0:31
  • Oh one problem I see now is that this is a get, but it's suppose to be a post. However. changing that didn't fix it. Commented Apr 1, 2018 at 0:32

2 Answers 2

15

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': []}}]
Sign up to request clarification or add additional context in comments.

Comments

3

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":[]}}]]'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.