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?
Advertisement
Answer
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': []}}]