I would like to use the Germany Rail (Deutsche Bahn) API and I am struggling translating the working example into R.
For instance I can get data for all stations similar to a name: https://developers.deutschebahn.com/db-api-marketplace/apis/product/fahrplan/api/9213#/Fahrplan_101/operation//location/{name}/get
I managed to register an APP etc., so that I can try the following get results with success.
GET https://apis.deutschebahn.com/db-api-marketplace/apis/fahrplan/v1/location/Berlin
Header:
Accept: application/json
DB-Client-Id: myid
DB-Api-Key: mykey
Now im am struggling to translate this into the GET
command of the httr
package
I am open to other solutions
I figured that I only need the first part for the url
url<-"https://apis.deutschebahn.com/db-api-marketplace/apis/fahrplan/v1/location/Berlin"
I found this link with explanations to add headers and other stuff https://httr.r-lib.org/reference/content_type.html
However, I am still struggling and not sure where I should go from something like this
r<-GET(url, add_headers(DB-Client-Id = client_id, `DB-Api-Key` = api_key)
accept_json())
I cannot manage to replicate the header, in particular for the add_headers
. Any help would be appreciated
CodePudding user response:
Based on the information you provided, it looks like you're going to want to hard code the Accept:
header in the add_headers()
argument like so.
library(httr)
r<-GET(url,
add_headers(Accept="application/json",
`DB-Client-Id` = "client_id",
`DB-Api-Key` = "api_key"))
For this should match the headers you need for your API request.
add_headers(Accept="application/json",
`DB-Client-Id` = "client_id",
`DB-Api-Key` = "api_key")
#Output
<request>
Headers:
* Accept: application/json
* DB-Client-Id: client_id
* DB-Api-Key: api_key
Hope this is helpful!