Vscode-restclient: [Feature request] Defaults added to all requests

Created on 22 Dec 2017  路  13Comments  路  Source: Huachao/vscode-restclient

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}}
enhancement

Most helpful comment

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": {
    }
}

All 13 comments

@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 \n with \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!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

heneds picture heneds  路  32Comments

codepunkt picture codepunkt  路  29Comments

chewlm86 picture chewlm86  路  13Comments

Fed29 picture Fed29  路  13Comments

johnbeynon picture johnbeynon  路  23Comments