Vscode-restclient: Getting parameter from a request

Created on 4 Jul 2020  路  11Comments  路  Source: Huachao/vscode-restclient

Hi! I'm trying to get a parameter value from an HTTP request that is generated randomly. This is my code:

#create auth user
# @name createUser
POST {{authServiceUrl}}/api/v1/users
content-type: application/json

{
    "email": "test-{{$guid}}@example.com",
    "passwordhash": "XXX"
}

@email = {{createUser.request.body.$.email}}

As a result I get the error:

Unexpected token @ in JSON at position 187

What is the correct way to do it?

Thanks in advance

Most helpful comment

### create auth user
# @name createUser
POST {{authServiceUrl}}/api/v1/users
content-type: application/json

{
     "email": "test-{{$guid}}@example.com",
     "passwordhash": "XXX"
}

### 
@email = {{createUser.request.body.email}}

1) Insert ### before @email =
2) and, in this case, without $

All 11 comments

@ygavrishov Please add a request delimiter ### before the email file variable definition as following

#create auth user
# @name createUser
POST {{authServiceUrl}}/api/v1/users
content-type: application/json

{
    "email": "test-{{$guid}}@example.com",
    "passwordhash": "XXX"
}

###

@email = {{createUser.request.body.$.email}}

Doesn't help... The same error

@ygavrishov did you meet this error when you try to send the request, could you please paste your request after modification?

### create auth user
# @name createUser
POST {{authServiceUrl}}/api/v1/users
content-type: application/json

{
     "email": "test-{{$guid}}@example.com",
     "passwordhash": "XXX"
}

### 
@email = {{createUser.request.body.email}}

1) Insert ### before @email =
2) and, in this case, without $

@kabrau do you mean you can only work when you remove the $ in the request variable path?

@ygavrishov in which case did you meet the following error?

Unexpected token @ in JSON at position 187

@kabrau do you mean you can only work when you remove the $ in the request variable path?

@ygavrishov in which case did you meet the following error?

Unexpected token @ in JSON at position 187

I used the snippet that you recommended. This is the only content of the file:

@authServiceUrl=...
#######################
#create auth user
# @name createUser
POST {{authServiceUrl}}/api/v1/users
content-type: application/json

{
    "email": "test-{{$guid}}@example.com",
    "passwordhash": "XXX"
}

###

@email = {{createUser.request.body.$.email}}
### create auth user
# @name createUser
POST {{authServiceUrl}}/api/v1/users
content-type: application/json

{
     "email": "test-{{$guid}}@example.com",
     "passwordhash": "XXX"
}

### 
@email = {{createUser.request.body.email}}
  1. Insert ### before @email =
  2. and, in this case, without $

Yes, it works!

@ygavrishov I still don't understand when you meet this error, sending the createUser request or hovering the email variable value

I have realized that both options (with or without $) work if I close the editor and open it again. Seems the error depends on previous invocations of the requests and variables' states.
Steps to reproduce the error:

  1. Open the saved document:
@authServiceUrl=...
#######################
#create auth user
# @name createUser
POST {{authServiceUrl}}/api/v1/users
content-type: application/json

{
    "email": "test-{{$guid}}@example.com",
    "passwordhash": "XXX"
}

### 
@email = {{createUser.request.body.$.email}}
  1. run it
  2. delete this part of the request:
### 
  1. run it
  2. restore the initial state
  3. run again. It fails

@ygavrishov so adding ### work for you, right? I'd like to close this issue. If you still have any questions, please feel free to reopen it.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

SimonDever picture SimonDever  路  15Comments

johnbeynon picture johnbeynon  路  23Comments

chewlm86 picture chewlm86  路  13Comments

akalcik picture akalcik  路  15Comments

olivier-voutat picture olivier-voutat  路  23Comments