'HTTP Request file in intellij, how to filter the first characters of the response to parse it
I am using the http client file to do requests in intellij (scratch.http), I want to parse the response and set a global variable. I saw here (https://www.jetbrains.com/help/idea/http-response-handling-examples.html#script-var-example) that I could use this:
POST https://httpbin.org/post
Content-Type: application/json
{
"token": "my-secret-token"
}
//Saving a variable
> {%
client.global.set("auth_token", response.body.json.token);
%}
But there is an issue, the endpoint I need to call returns the header Content-Type:application/json, but it's not really a json, because the response body starts with some characters, and then there is the json, so as an example:
)
]
}',
{
//normal json
}
how can I first slice the first characters of the response, and then parse the json to get the field that I want to set it as a global variable.
In postman I have this:
var jsonData = JSON.parse(responseBody.slice(5));
postman.setEnvironmentVariable("myField", jsonData.myField);
But I don't know how to do it with intellij http client.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
