# API-Test-Call with python

**URL:** <https://community.graylog.org/t/api-test-call-with-python/33193>\
**Category:** Development\
**Created:** [August 5, 2024, 10:13am UTC](https://community.graylog.org/t/api-test-call-with-python/33193 "2024-08-05T10:13:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ninjakaiden](https://avatars.discourse-cdn.com/v4/letter/n/ebca7d/32.png) [@ninjakaiden](https://community.graylog.org/u/ninjakaiden)\
**Post date:** [August 5, 2024, 10:13am UTC](https://community.graylog.org/t/api-test-call-with-python/33193/1 "2024-08-05T10:13:30Z")

</div>

## Hello Guys, at the moment i try to implement a small test-api-call with python (see below). With curl i get a valide response as json-object. With python i get a response, that my authorization ist not valid (401-Response.) See code below. Can somebody help me or give me some hints, thank you!:

import requests

api\_url=[https://172.168.98.208:9000/api/system?pretty=true](https://172.168.98.208:9000/api/system?pretty=true)’

bearer\_token=‘11111111111111111111’ #dummy-token

headers = {“Authorization:” “{bearer\_token}”}

response = requests.get(api\_url, headers=headers, verify=False)

## print(response.status\_code)

---

<div class="post-metadata">

**Author:** ![user023432](https://avatars.discourse-cdn.com/v4/letter/u/c0e974/32.png) [@user023432](https://community.graylog.org/u/user023432)\
**Post date:** [August 5, 2024, 8:16pm UTC](https://community.graylog.org/t/api-test-call-with-python/33193/2 "2024-08-05T20:16:21Z")

</div>

The api url isn’t formatted as a string properly.

---

<div class="post-metadata">

**Author:** ![drewmiranda-gl](https://sea2.discourse-cdn.com/flex016/user_avatar/community.graylog.org/drewmiranda-gl/32/13376_2.png) [@drewmiranda-gl](https://community.graylog.org/u/drewmiranda-gl)\
**Post date:** [August 5, 2024, 8:49pm UTC](https://community.graylog.org/t/api-test-call-with-python/33193/3 "2024-08-05T20:49:25Z")

</div>

The Graylog token is used with HTTP Basic authentication.

Here is an example:

```py
import requests
from requests.auth import HTTPBasicAuth
url = "http://hostname.domain.tld/api/"
d_headers = {"Accept":"application/json", "X-Requested-By":"python"}
username = 'apitoken'
password = 'token'
r = requests.post(url, headers=d_headers, verify=False, auth=HTTPBasicAuth(username, password))

```

Note that when using a token, the username is the contents of the graylog token, and password is `token`.

See also [Graylog REST API](https://go2docs.graylog.org/current/setting_up_graylog/rest_api.html) .

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex016/uploads/graylog/original/3X/c/7/c7c09c6b5099570133d6502b83f50ba4430de5b6.png) [@system](https://community.graylog.org/u/system)\
**Post date:** [August 19, 2024, 8:49pm UTC](https://community.graylog.org/t/api-test-call-with-python/33193/4 "2024-08-19T20:49:28Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
