Search code examples
rjsonapigethttr

RIS JSON API content compressed


I am trying to use the RIS API from the german railway. I managed to use the one on timetables that seems to be json. https://developers.deutschebahn.com/db-api-marketplace/apis/product/fahrplan/api/9213#/Fahrplan_101/operation/%2Flocation%2F{name}/get

GET https://apis.deutschebahn.com/db-api-marketplace/apis/fahrplan/v1/location/Berlin
Header:
Accept: application/json
DB-Client-Id: 36fd91806420e2e937a599478a557e06
DB-Api-Key: d8a52f0f66184d80bdbd3a4a30c0cc33

Which using httr I can replicate:

url_location<-"https://apis.deutschebahn.com/db-api-marketplace/apis/fahrplan/v1/location/Bonn"
r<-GET(url_location, 
       add_headers(Accept="application/json",
                   `DB-Client-Id` = client_id, 
                   `DB-Api-Key` = api_key))
content(r)

I know want to use another API about stations. https://developers.deutschebahn.com/db-api-marketplace/apis/product/ris-stations/api/ris-stations#/RISStations_160/operation/%2Fstations/get

GET https://apis.deutschebahn.com/db-api-marketplace/apis/ris-stations/v1/stations?onlyActive=true&limit=100
Header:
  Accept: application/vnd.de.db.ris+json
DB-Client-Id: 36fd91806420e2e937a599478a557e06
DB-Api-Key: d8a52f0f66184d80bdbd3a4a30c0cc33

I was hoping that it would work as well just adjusting the Accept:

url_station<-"https://apis.deutschebahn.com/db-api-marketplace/apis/ris-stations/v1/stations?onlyActive=true&limit=100"
r_stations<-GET(url_station, 
               add_headers(Accept="application/vnd.de.db.ris+json",
                           `DB-Client-Id` = client_id, 
                           `DB-Api-Key` = api_key))

I recieve some data and the status code is 200. It was 415 before adjusting the Accept

When I am looking at the content using the content function or without it I get the following

> head(content(r_stations), 30)
 [1] 7b 22 6f 66 66 73 65 74 22 3a 30 2c 22 6c 69 6d 69 74 22 3a 31 30 30 2c 22 74 6f 74 61 6c

r_stations$status_code
[1] 200

I should get something more like this:

{
  "offset": 0,
  "limit": 100,
  "total": 5691,
  "stations": [
    {
      "stationID": "1",
      "names": {
        "DE": {
          "name": "Aachen Hbf"
        }
      },

Solution

  • I just need to add type='application/json'

    content(r_stations, type='application/json')