Wednesday, 14 July 2021

How to get JSON object from link with Javascript Traverson HAL

I have the following JSON object:

{

  "_embedded" : {

    "carPriceStreams" : [ {

      "carClassCurrency" : {

        "carClass" : {

          "carClass" : {

            "code" : "A542",

          },

          "_links" : {

            "car" : {

              "href" : "http://localhost:9001/api/car/2"

            }

          }

        },

        "creationDate" : "2015-07-01"

      },

      "_links" : {

        "self" : {

          "href" : "http://localhost:9001/api/carPriceStreams/1"

        },

        "carPriceStream" : {

          "href" : "http://localhost:9001/api/carPriceStreams/1"

        },

        "carPrices" : {

          "href" : "http://localhost:9001/api/carPriceStreams/1/carPrices"

        }

      }

    },...

I make use of the Traverson JavaScript library and the HAL adapter for Traverson to get the object:

traverson.registerMediaType(TraversonJsonHalAdapter.mediaType, 
                     TraversonJsonHalAdapter);
var api = traverson
            .from(url + "/api")
            .jsonHal();
api
  .newRequest()
  .follow('carPriceStreams')
  .getResource(//...

For each carPriceStream, I want to follow links to its car and retrieve that car resource. How can I do this?

I've already tried

api
  .newRequest()
  .follow('carPriceStreams', 'car')
  .getResource(//...

But I get a HalLinkError.



from How to get JSON object from link with Javascript Traverson HAL

No comments:

Post a Comment