Visual Studio .http file request variable example

Request variables store values from a response for use in later requests. A common example is retrieving a Bearer token from one endpoint and using it to authenticate subsequent requests.

Here’s an example of extracting a value from a response body:


On line 11 we store the token value from the Login request and then we use this value on line 17.

Let’s see what the responses from the endpoints look like:


A token value of 1234567890 is returned from above and by examining the Request in the next response from the /todos endpoint we can see that same value has been passed in as the Bearer value in the Authorization header and thus our endpoint returns a 200.


Finally we can see we get a 401 when we pass in something other than the token we stored previously:


The endpoints themselves are just using hardcoded tokens to show that the token returned from the /users/token endpoint is stored and passed into the subsequent request to the /todos endpoint:


If you want the code above it’s available here →
https://github.com/davepcallan/HttpFileRequestVariableDemo

Leave a Reply

Your email address will not be published. Required fields are marked *