HTTP Request - Fails with 400 Bad Data

The content being posted is RAW with Json type

However the input result adds an extra comma on the end causing it to fail.
If I paste the content into Postman and remove the extra comma it posts successfully via Postman.

[
    {
        "ca": null,
        "qs": [],
        "url": "https://mycrm/Api/V8/module",
        "data": "{\r\n  \"data\": {\r\n    \"type\": \"Accounts\",\r\n    \"id\": \"myapikey\",\r\n    \"attributes\": {\r\n      \"adverse_media_check_c\": \"<table><tr><td><strong>Adverse Media Summary:</strong> ABC Limited has no known red flags or reputational concerns.</td></tr></table>\"\r\n    }\r\n  }\r\n}\r\n​"**,**
        "gzip": true,
        "method": "patch",
        "headers": [
            {
                "name": "Authorization",
                "value": "myaccesstoken"
            }
        ],
        "timeout": null,
        "useMtls": false,
        "authPass": null,
        "authUser": null,
        "bodyType": "raw",
        "contentType": "application/json",
        "serializeUrl": false,
        "shareCookies": false,
        "parseResponse": true,
        "followRedirect": true,
        "useQuerystring": false,
        "followAllRedirects": false,
        "rejectUnauthorized": true
    }
]

It’s the additional , at the end of the Data object that breaks it. It does not exist in the http request body.

If I remove all html content and just post “hello” it does post successfully.

Im at a loss … any help would be much appreciated

Welcome to the Make community!

Most of the time, you cannot directly map variables containing text content into another JSON string without escaping them, as the variable might contain characters that have a special meaning in JSON.

Special characters in a value like these needs to be specified as a “literal” character (instead of a special metacharacter), otherwise they make the whole JSON invalid when you map the variable value when creating another JSON string.

You can escape string variables by passing the text into the JSON “Transform to JSON” module —

Transforms any object to JSON.

Then, you can map the output of the “Transform to JSON” module into your JSON string. (Do note that when using the “Transform to JSON” module, the output should already contain the double quotes " " around your text, so when you map this output into your JSON, you should not need to wrap another pair of double quotes around it.)

Alternatively, you can use the built-in function replace to replace those special characters with their escaped versions, as discussed here.

For more information on escaping JSON strings, see: Understanding JSON Escape: A Comprehensive Guide

Hope this helps! Let me know if there are any further questions or issues.

— @samliew

2 Likes

@samliew as usual you are the master! Using the Transform to JSON module resolved this issue. Many thanks!

No problem, glad I could help you again!

1. If anyone has a new question in the future, please start a new thread. This makes it easier for others with the same problem to search for the answers to specific questions, and you are more likely to receive help since newer questions are monitored closely.

2. The Make Community guidelines encourages users to try to mark helpful replies as solutions to help keep the Community organized.

This marks the topic as solved, so that:

  • others can save time when catching up with the latest activity here, and
  • allows others to quickly jump to the solution if they come across the same problem

To do this, simply click the checkbox at the bottom of the post that answers your question:
Screenshot_2023-10-04_161049

3. Don’t forget to like and bookmark this topic so you can get back to it easily in future!

Hope this helps! Let me know if there are any further questions or issues.

— @samliew