It would be really convenient if there were some way to specify headers and parameters added to all requests. Maybe something like this:
@name=name
@pass=pass
DEFAULT token=123123&limit=100
Content-Type: application/json
Authorization: Basic {{name}} {{pass}}
It would then be possible to simplify further requests as:
GET http://some.path?param=p
Which would expand to:
GET http://some.path?param=p&token=123123&limit=100
Content-Type: application/json
Authorization: Basic {{name}} {{pass}}
@svenax I think this feature is not a general requirement, and also can be achieved by creating variable to store your default request part as follows(for Windows, the separator is rn, otherwise is n), use MacOS for instance:
@defaultRequestPart = token=123123&limit=100\nContent-Type: application/json\nAuthorization: Basic name pass
###
GET http://some.path?param=p{{defaultRequestPart}}
Let's wait to see if others have same requirements, if so I will consider to reopen it. Sorry for the inconvenience.
I think it would be very convenient for anyone using vscode-restclient to check/test many endpoints in an API. If you reopen the feature request others will have a greater chance to see it and have their say.
@svenax sure
Another reason for this is that variables are not expanded in other variables, so it is not possible to do e.g.:
@token = 123
@limit = 10
@default = token={{token}}&limit={{limit}}
Maybe that would also be useful.
How would this work for POST using e.g JSON?
@svenax Related to #208
For headers, this can kind of be accomplished with rest-client.defaultHeaders, but I just opened a feature request #237 to support environment variable replacements within those default headers.
But I can see a scenario where having default query parameters might be useful. E.g. if an API requires a client_id and client_secret to be passed with all requests. It's true you can define a variable containing those values and add them to your query strings, but that is sub optimal if every request requires it.
And I guess a different feature request would be to have different defaultHeaders (and defaultQueryParameters) by HTTP method. e.g.
rest-client.defaultHeaders {
"$shared:" {
},
"GET": {
},
"POST": {
}
}
@svenax I think this feature is not a general requirement, and also can be achieved by creating variable to store your default request part as follows(for Windows, the separator is rn, otherwise is n), use MacOS for instance:
@defaultRequestPart = token=123123&limit=100\nContent-Type: application/json\nAuthorization: Basic name pass ### GET http://some.path?param=p{{defaultRequestPart}}Let's wait to see if others have same requirements, if so I will consider to reopen it. Sorry for the inconvenience.
This solution is quite enough for me. But this information should be in the documentation.
Thank you.
@tonn is your OS Windows-based if so, replace \n with \r\n
@tonn is your OS Windows-based if so, replace
\nwith\r\n
Yes. The problem was in it.
So how to make a default header in http file
@defaultRequestPart=token=123123&limit=100\nContent-Type: application/json\nAuthorization: Basic name pass
@defaultHeaders=token=123123&limit=100\nContent-Type: application/json\nAuthorization: Basic name pass
@rest-client.defaultHeaders=User-Agent: rest-client\nAccept-Language: en-GB,en-US;q=0.8,en;q=0.6,zh-CN;q=0.4\nContent-Type: application/json
@defaultRequestPart token=123123&limit=100\nContent-Type: application/json\nAuthorization: Basic name pass
@defaultHeaders token=123123&limit=100\nContent-Type: application/json\nAuthorization: Basic name pass
@rest-client.defaultHeaders User-Agent: rest-client\nAccept-Language: en-GB,en-US;q=0.8,en;q=0.6,zh-CN;q=0.4\nContent-Type: application/json
@rest-client.defaultHeaders [User-Agent: rest-client\nAccept-Language: en-GB,en-US;q=0.8,en;q=0.6,zh-CN;q=0.4\nContent-Type: application/json]
# @rest-client.defaultHeaders User-Agent: rest-client\nAccept-Language: en-GB,en-US;q=0.8,en;q=0.6,zh-CN;q=0.4\nContent-Type: application/json
# @rest-client.defaultHeaders=User-Agent: rest-client\nAccept-Language: en-GB,en-US;q=0.8,en;q=0.6,zh-CN;q=0.4\nContent-Type: application/json
###
@baseUrl = http://localhost:32770
###
PUT {{baseUrl}}/lagou/_settings
{{defaultRequestPart}}
{
"number_of_replicas":2
}
###
PUT {{baseUrl}}/lagou/_settings
{
"number_of_replicas":2
}
###
GET {{baseUrl}}/lagou/_settings
Got response:
{
"error":"Content-Type header [] is not supported",
"status":406
}
@svenax I think this feature is not a general requirement, and also can be achieved by creating variable to store your default request part as follows(for Windows, the separator is rn, otherwise is n), use MacOS for instance:
@defaultRequestPart = token=123123&limit=100\nContent-Type: application/json\nAuthorization: Basic name pass ### GET http://some.path?param=p{{defaultRequestPart}}
Not ideal, but it gets the job done!
Most helpful comment
And I guess a different feature request would be to have different defaultHeaders (and defaultQueryParameters) by HTTP method. e.g.