Home > Blockchain >  IntelliJ http client save JSON value as variable not working for variable names with - character
IntelliJ http client save JSON value as variable not working for variable names with - character

Time:07-07

My GET Request:

### Bank token idclaim
GET {{IdClaimEndpoint}}/tokens/v1/id
Content-Type: application/json

> {% client.log(response.body.id-claim); %}

Response body:

{
  "documentation": "http://go/proofs",
  "id-claim": "eyJ0eXAiOiJKV1QiLCJhbGc"
}

Error from Response handler:

ReferenceError: "claim" is not defined. (<jsHandler script>#143)Script finished

If I try to extract value of documentation it works fine but not working for "id-claim".

I tried wrapping it within single/double quotes and also saving it to env variable and passing the env variable as {{id-claim}}, none of them worked.

CodePudding user response:

It is JavaScript. You can use response.body['id-claim'] syntax. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_Accessors#syntax

For example, the next request works fine:

POST https://httpbin.org/post
Content-Type: application/json

{
"my-json-value": "value"
}

{%
// 'json' is a property in HTTPBin response JSON
client.log(response.body.json["my-json-value"])
%}

CodePudding user response:

This works:

> {%
client.log(JSON.stringify(response.body.valueOf()).replace("\"documentation\":\"http:\/\/go\/proofs\",\"id-claim\":\"", "").replace("\"","").replace("{","").replace("}",""));
client.global.set("banktokenidclaim", JSON.stringify(response.body.valueOf()).replace("\"documentation\":\"http:\/\/go\/proofs\",\"id-claim\":\"", "").replace("\"","").replace("{","").replace("}",""));
%}

But is a lot of work for a simple thing. Not sure if there exists a easier/better solution.

  • Related