Skip to content
Advertisement

POST request to TRIAS API does not work with requests

I maintain an API client for the TRIAS API (German Link) to retrieve local public transport information for various states / cities in Germany. Recently one of the TRIAS servers (Baden-Württemberg) started responding with an error message to requests.

When I try to send the request via curl, the server responds just fine:

JavaScript

However, it fails with requests.post(), while it works with urllib.request.urlopen():

JavaScript

The respective code is:

JavaScript

Why does the request fail with requests.post() only? What can I do to debug this further? Other API servers respond fine to the request with requests.post()

Advertisement

Answer

It turned out to be the user agent. After inspecting the headers with tcpdump, I found, that the request fails with user agent python-requests/2.27.1 but succeeds with Python-urllib/3.10 and curl/7.83.1:

JavaScript
JavaScript
Advertisement