API Connect

 View Only
  • 1.  How can I transform a JSON response from an API to plain text?

    Posted Fri December 15, 2023 04:48 PM

    I currently have an api that in its response has a map node with the following configuration:

    and in the swagger it has the following property:

    However, the response I get when testing the API by postman always arrives in JSON format, the client is requesting that the response obtained be plain text.
    Can this be done? Can you help me with the code and execution?

    Thank you



    ------------------------------
    JIMMY VIVAS PERILLA
    ------------------------------


  • 2.  RE: How can I transform a JSON response from an API to plain text?

    Posted Mon December 18, 2023 03:46 AM

    Thats a pretty bad openapi definition - it gives the consumer absolutely no information about what the response looks like. Please encourage your customer to actually provide some proper documentation about the response object shape, what attributes it has etc...

    In terms of how to convert JSON to plaintext you can just use gateway script and:

    JSON.parse()

    If your API supports multiple response content types then the consumer can influence which one is used in the response via the Accept header. So if your API says it produces both json and plaintext and the consumer specifies Accept for JSON then your API must response with JSON. This is standard REST behaviour.



    ------------------------------
    Chris Dudley
    ------------------------------



  • 3.  RE: How can I transform a JSON response from an API to plain text?

    Posted Thu January 04, 2024 11:18 AM

    Thanks that help full.



    ------------------------------
    rana sab
    ------------------------------



  • 4.  RE: How can I transform a JSON response from an API to plain text?

    Posted Thu January 04, 2024 03:03 PM

    Hi Jimmy,
    You're saying your map policy is configured with an output for message.body that configures the output to have a content-type of text/plain but still has an inline schema that defines a JSON structure?  I believe the mapping will still create a JSON object for message.body but the message.headers.content-type should be text/plain although quite frankly I've never tested that use case, something I'll test out for sure.  Assuming the output from the map policy places a JSON object into message.body, the serialization of that object, either sent to some target-url in an invoke policy or returned to the client as the result of the API, should still return a serialized string.  Are you saying that the map policy is generating an application/json content type response header in this use case?
    Best Regards,
    Steve



    ------------------------------
    Steve Linn
    Senior Consulting I/T Specialist
    IBM
    ------------------------------



  • 5.  RE: How can I transform a JSON response from an API to plain text?

    Posted Thu January 04, 2024 03:18 PM

    Hi Jimmy,

    I just tested this with the following map configuration

          - map:
              title: map
              description: ''
              inputs: {}
              outputs:
                output:
                  schema:
                    type: object
                    properties:
                      hello:
                        type: string
                        name: hello
                      foo:
                        type: string
                        name: foo
                  variable: message.body
                  content: text/plain
              actions:
                - set: output.hello
                  default: world
                - set: output.foo
                  default: bar
              version: 2.1.0
              options:
                mapEmulateV4EmptyJSONObject: false
                mapEmulateV4DefaultRequiredProps: false
                mapEnablePostProcessingJSON: false
                mapReferenceLimit: 1

    The map policy will produce what it is asked to do, a JSON object that matches the inline output schema, specifically {"hello":"world","foo":"bar"}, but as expected the response content-type header is as configured in the map policy of text/plain since the output variable is message.body so message.headers.content-type will be set to the specified content-type value. Perhaps I'm not fully understanding your issue?
    Best Regards,
    Steve



    ------------------------------
    Steve Linn
    Senior Consulting I/T Specialist
    IBM
    ------------------------------