How to save response body of Intellij http client response to a file on local file system?
Asked Answered
C

2

6

In the response handling section

if below is the overall response

[
  {
    "empId":1001,
    "empName":"abc"
  },
  {
    "empId":1002,
    "empName":"xyz"
  }
]

I am able to get this response as

> {%

console.log(response.body);

%}

Is there any way to write this response to file on a local file system? Also, we seem to have access only to client and response objects. Can we also write control structures such as for loop, etc.?

Charlottecharlottenburg answered 6/5, 2020 at 0:0 Comment(0)
F
4

Not possible yet, here's a link to a corresponding feature request: https://youtrack.jetbrains.com/issue/IDEA-239333. You can vote/comment it to receive updates.

Edit: this is now possible, see PhpStorm docs on that topic.

Frothy answered 6/5, 2020 at 12:16 Comment(3)
The above feature request is to download an attachment from the api response. What i was looking for is to save resopnse body that is application/json in directory and filename of my choiceCharlottecharlottenburg
it also covers an option to save the response in a file you've specifiedFrothy
This is not true anymore. See answer from Ankit.Selfrestraint
P
13

You can redirect a response to a file. Use >> to create a new file with a suffix if it already exists and >>! to rewrite the file if it exists. You can specify an absolute path or relative to the current HTTP Request file. You can also use variables in paths, including environment variables and the following predefined variables:

{{$projectRoot}} points to the project root: .idea

{{$historyFolder}} points to .idea/httpRequests/

The following example HTTP request creates myFile.json in myFolder next to the HTTP Request file and redirects the response to it. If the file already exists, it creates myFile-1.json and so on.

POST https://httpbin.org/post
Content-Type: application/json

{
  "id": 999,
  "value": "content"
}

>> myFolder/myFile.json

ref:

  1. https://www.jetbrains.com/help/idea/exploring-http-syntax.html#response-redirect
  2. https://www.jetbrains.com/help/idea/http-response-handling-api-reference.html
Paniagua answered 1/12, 2021 at 8:16 Comment(0)
F
4

Not possible yet, here's a link to a corresponding feature request: https://youtrack.jetbrains.com/issue/IDEA-239333. You can vote/comment it to receive updates.

Edit: this is now possible, see PhpStorm docs on that topic.

Frothy answered 6/5, 2020 at 12:16 Comment(3)
The above feature request is to download an attachment from the api response. What i was looking for is to save resopnse body that is application/json in directory and filename of my choiceCharlottecharlottenburg
it also covers an option to save the response in a file you've specifiedFrothy
This is not true anymore. See answer from Ankit.Selfrestraint

© 2022 - 2024 — McMap. All rights reserved.