首页 > 解决方案 > Setting variables from the response in IntelliJ IDEA's HTTP request generator

问题描述

I am enjoying IntelliJ IDEA's HTTP request generator as an alternative to Postman etc.

Currently, my application uses an ephemeral key for JWTs. You know what kind of pain it is... Every time you restart the application you MUST authenticate again, even if the old token is still (temporally) valid.

Currently I have to run my OAuth authentication request with my credentials, copy-and-paste the JWT from the output JSON, and then paste in into next request's Authorization header

GET http://localhost:8080/api/auth/v1/token Authorization: CCB [... static token...]

{
  "id_token": "eyJhbGciOiJIUzI1NiJ9....", <== copy that!
  "refresh_token": "eyJhbGciOiJIUzI1NiJ9....",
  "expires_in": 1634292409144,
  "user_details": {

And

PATCH http://localhost:8080/api/v1/example/runImportJob Authorization: Bearer <== paste!

Question

I know that IntelliJ IDEA supports {{variables}}. I'd like to ask if it is possible to set the output of the token invocation into a variable which I'll then reference in the Authorization header

Desiderata

PATCH http://localhost:8080/api/v1/example/runImportJob
Authorization: Authorization {{jwt}}

And to run the authentication request (GET .../token) which ultimately sets the jwt variable, after jsonpath-ing the response of corse

标签: intellij-idea

解决方案


您可以尝试使用client.global.setclient.global.get来保存/加载变量。请参阅https://www.jetbrains.com/help/idea/http-response-handling-examples.html#script-var-example上的示例。


推荐阅读