How to convert the body of a TXT or XML request to JSON?

You can use native transformation interceptors to do this. With them, it’s possible to convert both the request and response body from XML or TXT to JSON and vice versa.

  1. Access your API or Plan and click on Edit;

  2. In the Flows section, select the Transformation tab;

  3. Click on the interceptor you want (XML to JSON and/or TXT to JSON) and drag it to the flow.

  4. A window will open with the interceptor’s notes. Click on Save;

  5. Click on Continue and Save to keep the changes.

Alternatively, you can also create a Custom JavaScript interceptor for this function.

Go to API Design > Interceptors and follow the steps:

  1. In the Custom Interceptors section, access your Custom JavaScript or create a new one;

  2. In the editor, enter the script:

    const objRequestBody = JSON.parse(strRequestBody);
  3. Click on Save to keep the change.

See the gif below with these steps:

convert body custom js
Thanks for your feedback!
EDIT

Share your suggestions with us!
Click here and then [+ Submit idea]