I’m trying to follow the code in this gist here: https://gist.github.com/arikfr/e3e434d8cfd7f331d499ccf351abbff9

However, I am unable to make it work. Specifically, this line is the one creating problems:

response = s.post('{}/api/queries/{}/refresh'.format(redash_url, query_id), params=params)

I get a status code 200 but when I call response.json() I get an error: in fact, if I inspect the headers, Content-Type is not application/json but text/html instead, and inspecting response.text I get a bunch of meaningless HTML tags, with no actual content inside.

I am using my own user API key (admin one) and this is what my params look like:

params = {
    'parameters': {
        'retailer': 'client123',
        'date_range_param': {
            'start': '2020-06-11',
            'end': '2020-06-24'
        }
    }
}

I am lost, I tried all possible solutions I could find around but got no progress. Any help would be very appreciated.

Edit: For the record, other requests work normally. If I call either /api/queries or /api/queries/, I get the expected response.

Have you tried using the API documentation directly? That gist is quite old. The /refresh endpoint was replaced with /results.

Remember that running queries with parameters is an asynchronous process. You’ll basically always need more than one network request to get there results. The first request kicks off the execution. The second request checks the status of the execution. Repeat the second request until a query_result_id is returned. The third request actually retrieves the query result contents.

Here is a simple a simple example using the results endpoint.

First Request

Post to /api/queries/1010101/results
Request body JSON looks like this:

{
"parameters": 
        "retailer": "client123",
        "date_range_param": {
            "start": "2020-06-11",
            "end": "2020-06-24"
        }
    }
}

The response includes a job_id.

Second Request

Get to /api/jobs/<job_id_from_request_one>
When the query finishes executing this request will return a query_result_id.

Third Request

Get to /api/query_results/<query_result_id_from_request_two>.json

Returns the JSON data.

I started from documentation but not being able to make it work I went back in time. I meant to use the gist from redash toolbelt.

Anyway, when I use the /results API, I get a 404 that says: “Hey, there’s not any cached job for this query etc… maybe you should try the “/refresh” endpoint”. Which is what let me to try also using /refresh.

I understood that refreshing a query with parameters is a 2 step job, problem is, I can’t manage to make it go and produce new results. I don’t get a new job ID from the first request :confused:

Actually forget everything I said. Apparently it is a problem with python “requests” library and not with the API itself.

I tried with curl and it’s working perfectly so w/e, I’ll figure out what was wrong with python. Nvm thanks!

For the record, apparently the problem was into the r.post arguments: instead of passing:
params = params

I had to pass
data = json.dumps(params).

1 Like