Huachao / vscode-restclient

REST Client Extension for Visual Studio Code
https://marketplace.visualstudio.com/items?itemName=humao.rest-client
MIT License
5.08k stars 423 forks source link

Request Variables #1232

Open MicHamoNZ opened 6 months ago

MicHamoNZ commented 6 months ago

Hi, just getting started with this extension and finding it very useful. Thanks

I am struggling with extracting parts of my response to a Request Variable as I don't seem to be able to get the JSONPath / XPath correct.

@name randomUser

GET https://randomuser.me/api/ Content-Type: application/json

May named request '@randomUser', returns the following response Body. { "results": [ { "gender": "female", "name": { "title": "Miss", "first": "Evie", "last": "Walker" }, "email": "evie.walker@example.com", "dob": { "date": "1966-01-22T05:24:59.554Z", "age": 57 }, "phone": "(395)-108-2591", "cell": "(939)-256-1145" } ], "info": { "seed": "04e5308488436fe0", "results": 1, "page": 1, "version": "1.4" } }

I have tried a number of ways to extract the gender using a Request Variable assignment. for use in my next request however, as soon as I uncomment this code I get a 'Bad Request' error and hovering over the line of text I get various errors.

@gender = {{randomUser.response.body.$.gender}} @gender = {{randomUser.response.body.result[0].gender}}

Are you able to help

natalie-o-perret commented 6 months ago

Hello,

Here is an example using the name instead,

### Get a random fake user
# @name randomUser
GET https://randomuser.me/api/
Content-Type: @jsonContentType
###

### Guess the nationality based on a name
# @name nationalizeAuto
GET https://api.nationalize.io?name={{randomUser.response.body.$.results[0].name.first}}
Content-Type: @jsonContentType
###

Sending the first request: image

and then the second one: image


Anyway, I suspect that in your case:

I have tried a number of ways to extract the gender using a Request Variable assignment. for use in my next request however, as soon as I uncomment this code I get a 'Bad Request' error and hovering over the line of text I get various errors.

@gender = {{randomUser.response.body.$.gender}} @gender = {{randomUser.response.body.result[0].gender}}

It's results, plural, also you need to use the $ to fetch the relevant json field:

{{randomUser.response.body.$.results[0].gender}}

Hope this can help, merry xmas! πŸ€Άβ„οΈπŸŽ„πŸŽ