Reputation: 18
I am trying to use an API from trafikverket.se to get current air temperatures in Python. I've registered an account and got a key to use for my request. But even though I am trying their example code I can not get it to work.
API request documentation trafikverket.se
my python code:
import requests
data = """
<REQUEST>
<LOGIN authenticationkey="8c981e556c........." />
<QUERY objecttype="WeatherStation" schemaversion="1">
<FILTER>
<EQ name="Name" value="Högakustenbron Topp" />
</FILTER>
<INCLUDE>Measurement.Air.Temp</INCLUDE>
<INCLUDE>Measurement.MeasureTime</INCLUDE>
</QUERY>
</REQUEST>
"""
#data = data.encode('utf8')
print(requests.post('https://api.trafikinfo.trafikverket.se/v2/data.json', data=data).json())
I have tried it with and without the encode to utf8 since I found someone else that had a similar problem and that solved it for them. I only get a strange response:
{'RESPONSE': {'RESULT': [{'ERROR': {'SOURCE': 'Request', 'MESSAGE': 'Header Content-Type is invalid.'}}]}}
if i use the response query on their test console i get the correct response:
{
"RESPONSE": {
"RESULT": [
{
"WeatherStation": [
{
"Measurement": {
"MeasureTime": "2022-11-11T15:40:00.000+01:00",
"Air": {
"Temp": 11.3
}
}
}
]
}
]
}
}
Upvotes: 0
Views: 166
Reputation: 1108
From the documentation you linked to, it looks like they require a Content-Type
header (translated to English):
Content-Type
As of version 2, the POST call must have one of the following values in the Content-Type header:
application/xml text/xml text/plain (triggers no CORS preflight, more info here .)
Original:
Content-Type
Från och med version 2 måste POST-anropet ha någon av följande värde i Content-Type headern:
application/xml text/xml text/plain (triggar ingen CORS preflight, mer info finns här.)
I would suggest trying the following:
print(requests.post('https://api.trafikinfo.trafikverket.se/v2/data.json', data=data, headers={'Content-Type': 'text/xml'}).json())
If text/xml
does not work, then I would suggest trying application/xml
or text/plain
in its place.
Upvotes: 1