Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

curl: per-file additional headers from CLI (for multipart POST, SOAP, etc.)

I try to add Content-ID: as long as some other custom headers, using curl and command-line only (headers are should not come from file), and they are should be different for each attachment in same request. But currently i am able to specify only content-type(s) per file basis (it is documented). I did not found any example of documentation for my task.

I use something like:

curl -X POST --form "[email protected]" --form "[email protected]" http://host:port

And result is following (reconstructed with wireshark). There is no TEST words by default (see below).

POST / HTTP/1.1
Host: host:port
User-Agent: curl/7.52.1
Accept: */*
Content-Length: 408
Expect: 100-continue
Content-Type: multipart/form-data; boundary=------------------------d7a9fdc4812ebc3b

 [SERVER REPLY:]  HTTP/1.1 100 Continue

--------------------------d7a9fdc4812ebc3b
Content-Disposition: form-data; name="file"; filename="test1.txt"
Content-Type: text/plain
TEST  <-- This is from _curl_ C code bruteforce. Here should be "Content-ID:from_command_line_for_file_1"

CONTENTS_OF_test1.txt
line2
line3

--------------------------d7a9fdc4812ebc3b
Content-Disposition: form-data; name="file"; filename="test2.jpg"
Content-Type: image/jpeg
TEST  <-- This is from _curl_ C code bruteforce. Here should be "Content-ID:from_command_line_for_file_2"

CONTENTS_OF_test2.jpg
line2
line3

--------------------------d7a9fdc4812ebc3b--

To see if it possible with curl, i dive into its source code. There is at 'formdata.c' line ~1290, code for my exactly task. But how to activate this code? To confirm if this code really for per-file processing, i bruteforce it for testing, and it works (see above for 'TEST' words).

     curList = file->contentheader;
      while(curList) {
    /* Process the additional headers specified for this form */
    result = AddFormDataf(&form, &size, "\r\n%s", curList->data);
    if(result)
      break;
    curList = curList->next;
      }
// my bruteforce added line for test:
      result = AddFormDataf(&form, &size, "\r\n%s", "TEST");

So it looks like my task already supported by curl, but how to use it, and which parameters should i pass via command line (not with / from some file)?

man curl does not suggest too much about that, but it says 'See further examples and details in the MANUAL', but i can't find there something helpful.

curl 7.52.1

Thanks.

like image 485
jpka Avatar asked Feb 10 '17 01:02

jpka


2 Answers

In curl 7.58.0, you can now add headers to a part in a multipart request like so:

curl -F "[email protected];headers=\"X-My-Hdr: 42\"" http://host.com/path
like image 90
elhefe Avatar answered Oct 05 '22 14:10

elhefe


I receive the answer on curl's mailing list. The curl CLI tool uses libcurl library. 'formdata.c' is from a library. And it already contains all needed code. But unfortunately, curl tool does not use this particular (which is in question) feature of library. This is the reason why mentioned part of code can not be triggered at all (by CLI tool).

So i am out of luck, but know that it is impossible, is better than do not know at all. BTW, i then try to modify curl CLI tool source code, but found that it is far not easy for me. Any suggestions are welcome. Thanks

like image 38
jpka Avatar answered Oct 05 '22 15:10

jpka