Search code examples
jsonhttpsyntaxinsomnia

API-POST error: ' Syntax Error: Cannot parse the unexpected character \"\\\\\"."


I am trying to extract data from a web site, using Insomnia, by POSTing an API-request that I've copied directly from the specific request (via cURL (bash)).

Here is the JSON (which I assume is causing the error?):

{
"operationName": "Holdings",
"variables": {
"countrycode": "DE",
"languagecode": "de_DE",
"fundid": "27854"
},
"query": "query Holdings($fundid: String\\u0021, $countrycode: String\\u0021, $languagecode: String\\u0021) {\\n  Portfolio(\\n    fundid: $fundid\\n    countrycode: $countrycode\\n    languagecode: $languagecode\\n  ) {\\n    fundname\\n    producttype\\n    assetclass\\n    portfolio {\\n      topholdings {\\n        asofdate\\n        hldngname\\n        geocode\\n        sectorname\\n        brkdwnpct\\n        frequency\\n        calcbasislocal\\n        calctypelocal\\n        allocflag\\n        hasderivatives\\n      }\\n      dailyholdings {\\n        asofdate\\n        frequency\\n        secticker\\n        isinsecnbr\\n        cusipnbr\\n        secname\\n        quantityshrpar\\n        origcouponrate\\n        sctrname\\n        pctofnetassets\\n        mktvalue\\n        notionalmktvalue\\n        secexpdate\\n        assetclasscatg\\n        mktcurr\\n        contracts\\n      }\\n      fullholdings {\\n        asofdate\\n        asofdatestd\\n        frequency\\n        secticker\\n        isinsecnbr\\n        cusipnbr\\n        secname\\n        quantityshrpar\\n        origcouponrate\\n        sctrname\\n        pctofnetassets\\n        mktvalue\\n        notionalmktvalue\\n        secexpdate\\n        assetclasscatg\\n        mktcurr\\n        contracts\\n        finalmaturitydate\\n        investmentcategory\\n      }\\n    }\\n  }\\n}\\n"
}

Here is the error-message that I keep on getting:

"errors": \[
"Syntax Error: Cannot parse the unexpected character "\\\\"."
\]

I can't seemed to find this syntax error, and also don't understand how a syntax error could be made if the cURL have been directly copied?

Any help will be great!

I've checked the query-URL as well as headers, and these seem to be fine, and also similar to what I've used in the past (with success).


Solution

  • A newline is formatted as \n instead of \\n.

    If you do a double backslash \\, then you'll escape the second backslash \, rendering it literal. Although this looks intentional between the String and unicodes. (String\\u0021), as you probably want a single backslash there.

    A single backslash is pretty sensitive in escaping itself and breaking the code, so some programs have to take self-measures to escape a backslash with another backslash to parse it correctly. It's always worth to double-check the backslashes by generated code.