How to construct REST API to Support both XML and JSON format .

When the consumer makes a Rest Call  to Provider, Provider  Should return the response depends on consumer Request.How to construct  REST API  to Support both XML and JSON.

  Discussion posts and replies are publicly visible

Parents
  • You may want to do something like this below: 

     

    /* Assuming the consumer request passes a parameter contentFormat with values either JSON or XML

    * Get the requestFormat

    */

    local!requestFormat: nvl(http!request.queryParameters.contentFormat."JSON") 

     

    /* Set contentType to set in response based on request Format 

    */

    local!contentType: if( local!requestFormat="JSON",  "application/json", "application/xml")

     

    /

    *

    * Do some application logic and set local!data as data to be sent back as response.

    *

    */

    a!httpResponse(

       /*

       * set content-type based on local!contentType

       */

       headers: {

         a!httpHeader(name: "Content-Type", value: local!contentType)

       },

       /*

       * use tojson or toxml based on  requestFormat

       */

       body: if( local!requestFormat="JSON", a!toJson(local!data),  toxml(local!data))

    )

Reply
  • You may want to do something like this below: 

     

    /* Assuming the consumer request passes a parameter contentFormat with values either JSON or XML

    * Get the requestFormat

    */

    local!requestFormat: nvl(http!request.queryParameters.contentFormat."JSON") 

     

    /* Set contentType to set in response based on request Format 

    */

    local!contentType: if( local!requestFormat="JSON",  "application/json", "application/xml")

     

    /

    *

    * Do some application logic and set local!data as data to be sent back as response.

    *

    */

    a!httpResponse(

       /*

       * set content-type based on local!contentType

       */

       headers: {

         a!httpHeader(name: "Content-Type", value: local!contentType)

       },

       /*

       * use tojson or toxml based on  requestFormat

       */

       body: if( local!requestFormat="JSON", a!toJson(local!data),  toxml(local!data))

    )

Children
No Data