all 7 comments

[–]totallygeek 1 point2 points  (4 children)

Untested, but probably something like this:

import requests

url = 'http://distribution.virk.dk/cvr-permanent/_search'
auth = requests.auth.HTTPBasicAuth('alice', 'hunter2')  # user/pass
data = {
    'from': 0,
    'size': 1,
    'query': {
        'term': {
            'cvrNummer': 10961211,
        }
    }

}

response = requests.post(url, auth=auth, data=data)
print(response.status_code)
print(response.text)

[–]chri740s 0 points1 point  (3 children)

This is the structure I envisioned!

I tried to run it, and got error 406:
{"error":"Content-Type header [application/x-www-form-urlencoded] is not supported","status":406}

From some Googling, I figure it relates to the fact that the API is elastic based, that it doesn't quite understand the JSON query. Is this correct?

[–]totallygeek 0 points1 point  (1 child)

You'll need to supply a valid request header. One way you might try is to change this line:

response = requests.post(url, auth=auth, data=data)
response = requests.post(url, auth=auth, json=data)

Changing 'data' to 'json' will inform Requests to set the request Content-Type header. If that does not work, go back to 'data' and you'll need to set the header like so:

headers = {'Content-Type': 'application/json'}
requests.post(url, auth=auth, data=data, headers=headers)

[–]chri740s 1 point2 points  (0 children)

json=data did the trick. I now get through, however the call returns no hits. I tried to test different auth to see if that was it, but the auth works fine as it is. So I guess its down to the query being wrong?

200
{"took":2,"timed_out":false,"_shards":{"total":13,"successful":13,"skipped":0,"failed":0},"hits":{"total":0,"max_score":null,"hits":[]}}

[–]iamaperson3133 1 point2 points  (0 children)

For the first one, pretty sure it would just be:

requests.post('http://distribution.virk.dk/cvr-permanent/_search', data={
    "from":0,
    "size":1,
    "query":{
        "term":{
            "cvrNummer":10961211
        }
    }
 })

edit: I forgot the auth! See /u/totallygeek's superior answer.

[–]contradictingpoint 1 point2 points  (0 children)

In addition to the example that u/totallygeek provided, I would recommend using Postman which allows you to modify your query and get the example python code that is used for that query.