最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

httpclient - Use dynamically generated filename in IntelliJ HTTP Client multipart request? - Stack Overflow

programmeradmin1浏览0评论

I am using the IntelliJ HTTP Client to send a multipart POST request and would like to dynamically generate a filename in the pre-request script and pass it to the Content-Disposition header inside the request body.

I have a working request with a hardcoded filename:

#@name Upload file 
< {%
let myFileName = "file414143.pdf"; 
request.variables.set("myFileName", myFileName);
%}
POST {{url}}/files 
Authorization: Basic {{credentials}}
Content-Type: multipart/form-data; boundary=WebAppBoundary

--WebAppBoundary
Content-Disposition: form-data; name="file"; filename="file.pdf"
Content-Type: application/pdf

< ../myfile.pdf
--WebAppBoundary--

Now, I am trying to replace the static filename with the dynamically generated one by referencing the variable in the request body:

#@name Upload file - with variable
< {%
let myFileName = "file414143.pdf"; 
request.variables.set("myFileName", myFileName);
%}
POST {{url}}/files 
Authorization: Basic {{credentials}}
Content-Type: multipart/form-data; boundary=WebAppBoundary

--WebAppBoundary
Content-Disposition: form-data; name="file"; filename="{{myFileName}}"
Content-Type: application/pdf

< ../myfile.pdf
--WebAppBoundary--

This approach does not work, and the filename is not replaced with the expected value from request.variables.get("myFileName").

How can I correctly use a dynamically generated filename inside the Content-Disposition header of a multipart request in IntelliJ HTTP Client?

Is there a proper way to reference variables inside the multipart request body, or is this not supported?

I expected the variable {{myFileName}} to be replaced with the value set in the pre-request script (file414143.pdf) when executing the request. The expected result was a Content-Disposition header like this:

Content-Disposition: form-data; name="file"; filename="file414143.pdf"

However, instead of the expected filename, it appears that the variable is not resolved correctly, and the request fails. The filename remains as {{myFileName}} in the request payload rather than being substituted with its actual value.

I am using the IntelliJ HTTP Client to send a multipart POST request and would like to dynamically generate a filename in the pre-request script and pass it to the Content-Disposition header inside the request body.

I have a working request with a hardcoded filename:

#@name Upload file 
< {%
let myFileName = "file414143.pdf"; 
request.variables.set("myFileName", myFileName);
%}
POST {{url}}/files 
Authorization: Basic {{credentials}}
Content-Type: multipart/form-data; boundary=WebAppBoundary

--WebAppBoundary
Content-Disposition: form-data; name="file"; filename="file.pdf"
Content-Type: application/pdf

< ../myfile.pdf
--WebAppBoundary--

Now, I am trying to replace the static filename with the dynamically generated one by referencing the variable in the request body:

#@name Upload file - with variable
< {%
let myFileName = "file414143.pdf"; 
request.variables.set("myFileName", myFileName);
%}
POST {{url}}/files 
Authorization: Basic {{credentials}}
Content-Type: multipart/form-data; boundary=WebAppBoundary

--WebAppBoundary
Content-Disposition: form-data; name="file"; filename="{{myFileName}}"
Content-Type: application/pdf

< ../myfile.pdf
--WebAppBoundary--

This approach does not work, and the filename is not replaced with the expected value from request.variables.get("myFileName").

How can I correctly use a dynamically generated filename inside the Content-Disposition header of a multipart request in IntelliJ HTTP Client?

Is there a proper way to reference variables inside the multipart request body, or is this not supported?

I expected the variable {{myFileName}} to be replaced with the value set in the pre-request script (file414143.pdf) when executing the request. The expected result was a Content-Disposition header like this:

Content-Disposition: form-data; name="file"; filename="file414143.pdf"

However, instead of the expected filename, it appears that the variable is not resolved correctly, and the request fails. The filename remains as {{myFileName}} in the request payload rather than being substituted with its actual value.

Share Improve this question asked Feb 4 at 13:25 MoritzMoritz 211 silver badge1 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 0

It works for me when I specify the filename without quotes. Instead of

Content-Disposition: form-data; name="file"; filename="{{myFileName}}";

use

Content-Disposition: form-data; name="file"; filename={{myFileName}};

According to MDN you may not need the quotes:

The quotes around the file name are optional, but are necessary if you use special characters in the file name, such as spaces.

If you want to preserve the quotes, you could put the whole header value in a variable:

< {%
    let myFileName = "file414143.pdf";
    request.variables.set("contentDisposition", "form-data; name=\"file\"; filename=\"" + myFileName + "\";");
%}
...
Content-Disposition: {{contentDisposition}}

I'd still file a bug report with Jetbrains, though.

发布评论

评论列表(0)

  1. 暂无评论