Open sebastienlevert opened 1 month ago
Should we consider some sort of auth as part of the snippet?
@maisarissi we could, based on the auth schemes and have the right Auth headers. What would be your suggestion on automating this?
I would suggest:
1 - use the settings.json
file. So instead of the bellow code in the .http snippet file:
# Base URL
@url = https://jsonplaceholder.typicode.com
I would suggest the .http snippet file be:
### GET /posts?userId={userId}{&title}
# Filter results by user ID
@userId = 1
# Get posts
GET {{HostAddress}}/posts?userId={{userId}}
### POST /posts
# Create posts
POST {{HostAddress}}/posts
Content-Type: application/json
{
"title": "",
"body": "",
"userId": 1
}
and add a rest-client.environmentVariables
entry in the settings.json
file with a remote
or prod
node:
{
"rest-client.environmentVariables": {
"$shared": { },
"remote": {
"hostAddress": "https://jsonplaceholder.typicode.com",
"basicAuth": "Basic dXNlcm5hbWU6c2VjcmV0",
"bearer": " Bearer AAAACSSXSXSV....SDSDSD"
}
}
}
This would allow folks to easily add as many environments as needed (like dev) later and then change easily the env that is been called, like the screenshot below:
If we go that way, we need to remove the @url
from the .http
file, because variable definitions in .http files override environment definitions.
2 - For the auth, based in auth scheme described in the OpenAPI description, I believe we should create/add the environment variables in the settings.json
. So for example, if the /posts
requires Basic auth, the .http file should be:
POST {{HostAddress}}/posts
Content-Type: application/json
Authorization: {{basicAuth}}
{
"title": "",
"body": "",
"userId": 1
}
3 - I would say we should support "basicAuth", "bearer" and "apiKey". For Oauth2, we might want to also use "bearer" where the user would need to use a browser to authenticate and copy the token.
Another possibility would be to have something like this blog where we use info from the settings.json
@name oauth2
POST {{accessTokenUri}}?grant_type=client_credentials&client_id={{clientId}}&client_secret={{clientSecret}}
Content-Type: application/x-www-form-urlencoded
###
GET https://{hostAddress}/api/endpoint HTTP/1.1
Authorization: Bearer {{oauth2.response.body.access_token}}
Based on this OpenAPI : https://github.com/sebastienlevert/jsonplaceholder-api/blob/main/openapi.yaml
(I made edits to the operation to illustrate some use cases.)
And this operation:
The format should be the following: