It isn't clear for me if and how can I use variables inside of HTTP file like this:
{
"host": "https://localhost:44300"
}
POST {{host}}/connect/token HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Cache-Control: no-cache
@akalcik you should first create custom variables in the vscode settings file, like following:
"rest-client.environmentVariables": {
"dev": {
"host": "https://localhost:44300"
}
}
This piece of code adds a new environment named dev with a custom variables host. The environment name and variable name is whatever you like.
After that, in your http file write the request as before:
POST {{host}}/connect/token HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Cache-Control: no-cache
Finally, switch to the environment named dev as defined in setting file, to switch environment just press Ctrl+Alt+E(Cmd+Alt+E for macOS), or press F1 and then select/type Rest Client: Switch Environment. Or even simple to click the No Environment button in the right bottom status bar.
Ok, the question was If I can define variables directly in _http_ file. It will more easy than to share file with others.
@akalcik sorry I didn't understand what you mean, and I know what your pain is. When I implement this feature, my main goals are:
http file of specified environmentAnd I think your requirement to __share__ is also useful, I will consider this carefully.
It would be very nice to have.
For example emacs's restclient does it very well (it's called in-buffer variables):
https://github.com/pashky/restclient.el#in-buffer-variables
+1 this will be a great feature 馃憤
One more use case will benefit from variables defined locally in .http file:
If app has pretty complex auth process with several redirects, origins checks and user actions it will be hard to recreate full process in REST Client, but it is pretty easy to use session cookie that I can obtain from browser dev tools. Since most of requests require session cookie it will be nice to have single location to change it.
Since this variable always changes it has no value to be declared in environment settings however host still be uses from environment
Pseudo code:
### Variable {{sessionCookie S0M3S3SS10NC0OK13}}
### Obtain User location
GET http://{{host}}/user/location HTTP/1.1
Cookie: SESSIONID={{sessionCookie}}
### Obtain User roles
GET http://{{host}}/user/roles HTTP/1.1
Cookie: SESSIONID={{sessionCookie}}
@akalcik @finalclass @richarddavenport @dakaraphi @ricardocosta89 @IllusionMH @devalnor @dymo @neilsmind @viraf @wprater I have implemented the feature that define in file http variables in latest version 0.15.0, you can try it and any suggestions and feedback are welcome. The details can be found here
I have just installed the latest package. The local variable feature is really useful. I have been waiting for this for long time. I have tested and its working as expected. :)
It will save a lot for me.
@mgpatel75 really happy that you enjoy it
@Huachao checked several use cases and it works as expected. Thank you!
One little question: is there a way to add line break into variable (not a multiline variable declaration, but receive unescaped \n on the server) and sent in in the POST message body?
@IllusionMH I'd like to introduce the escape character \ to support this feature, like:
@location = Shanghai\nChina
Do you think it's ok for you?
@Huachao thank you! Yes, this will work.
@IllusionMH you can try the latest version 0.15.1 to verify the escape feature
@Huachao yes, looks good!
Also with this escape it is possible now to define common headers for your requests (may be useful if API has required set for headers for different endpoints)
###
@commonHeaders=Accept: application/json\r\nContent-type: application/json; charset=UTF-8\r\nCookie: SESSIONID=
@sessionId = 2BC49FA936237E0CEE422AB9A3134519
###
GET http://localhost:1337/rest/users HTTP/1.1
{{commonHeaders}}{{sessionId}}
###
GET http://localhost:1337/rest/places HTTP/1.1
{{commonHeaders}}{{sessionId}}
is there a way for me to have scoped local variable to a particular request
like for example
// request 1
@name=john
GET http://localhost/{{name}} // produces http://localhost/jack
// request 2
@name=jack
GET http://localhost/{{name}} // produces http://localhost/jack
as u see the in request 1 local variable name is overwritten with the new value "jack" when the name is redifined in request 2
is there any way to prevent this from happening?
Most helpful comment
@akalcik @finalclass @richarddavenport @dakaraphi @ricardocosta89 @IllusionMH @devalnor @dymo @neilsmind @viraf @wprater I have implemented the feature that define in file http variables in latest version 0.15.0, you can try it and any suggestions and feedback are welcome. The details can be found here