Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use script to form request body in IntelliJ integrated http client

In pseudo-whish-language this is what I try to achieve in IntelliJ integrated http client:

POST {{basepath}}/upload
Content-Type: application/json

{
  "content": "{% btoa(await fetch('my-file.dat')) %}"
}

Normally, one would use the following to upload a file raw or as part of a multipart request:

< my-file.dat

But in my case, the binary file has to be encapsulated in a json and encoded using base64. I tried putting the file into a variable, but I can't find a way to run a script (for setting the variable) before the request is sent and also it doesn't seem possible to use script directly as part of the request. And I'm also not sure if I'm able to access external files from the script.

I cannot send it as multipart as proposed in Add file to multipart form request in IntelliJ HTTP Client because my server doesn't accept non-json requests.

Do I have any other options or is this a missing feature of this http client?

like image 383
Daniel Alder Avatar asked Nov 20 '25 06:11

Daniel Alder


1 Answers

I did some tricks to achieve something similar.

  • I created an Environment Variable with the data parsed to base64 format.
{
  "dev": {
    "png_image_base64": "--your format data in bae64--"
   }
}
  • Then, I added this as a reference in my HTTP request.
### ADD FILE TO TRACKING
POST http://localhost/files
Content-Type: application/json
Authorization: Bearer {{personal_access_token}}

{
  "file": "{{png_image_base64}}"
}
  • Finally, run the request with the dev environment.

run HTTP request with environment variables

You could create many vars with different files of content: pdf_base64, png_bas464, etc. depending on your necessities. The "bad thing" is that you need to parse manually each of these files, but only once; after that, you could use it on a json and perform many tests.

like image 195
hizmarck Avatar answered Nov 23 '25 13:11

hizmarck



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!