Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set HTTP requests defaults in IntelliJ HTTP request client editor

In IntelliJ http request editor; is there a way to set the common config for all the requests in the file (and globally) ?

For example I would like to specify an authorization header for all the requests.

Current code

GET http://localhost:8080/api/foo
Authorization: Bearer my-token

The code I am trying to achieve:

Desirable code

<common headers>
Authorization: Bearer my-token

GET http://localhost:8080/api/foo
GET http://localhost:8080/api/bar
GET http://localhost:8080/api/baz

like image 366
ps-aux Avatar asked Mar 04 '23 23:03

ps-aux


1 Answers

It's a bit late but i will try to give an answer in case someone is coming around.

I'm not sure it is exactly what you are looking for but maybe it help.

If you are getting your token dynamically from a login endpoint, you can store the token in a variable and use it later in any request.

Exemple:

### Login

POST http://localhost:8080/login
Content-Type: application/json

{
  "email": "someEmail",
  "password": "somePassword"
}

> {%
    client.global.set("auth_token", response.headers.valuesOf('x-auth-token')[0]);
  %}

### Get user

GET http://localhost:8080/user/someUserId
Authorization: Bearer {{auth_token}}

In this case, i store my token coming from the header x-auth-token in a variable auth_token. Than i use it in the authorization header for all my next requests.

Found from the official JetBrains website HTTP response handling exemples

Have a nice day!

like image 105
Le Poulet Suisse Avatar answered Mar 10 '23 10:03

Le Poulet Suisse