0

I want to replace this:

curl -F 'file=@a.html' http://localhost:8080/convert

with

curl -F 'file=<html><body>Inline!</body></html>' http://localhost:8080/convert

but that just gets me: curl: (26) couldn't open file "html><body>a</body></html>"

using curl -d doesn't work (presumably because it doesn't generate a multipart body?)

How can I embed content in the curl parameter instead of relying on a reference to an actual file?

Chris F Carroll
  • 11,146
  • 3
  • 53
  • 61

2 Answers2

2

use the --form-string argument instead of -F, eg

curl --form-string 'file=<html><body>Inline!</body></html>' http://localhost:8080/convert

which yields

POST /convert HTTP/1.1
Host: localhost:8080
User-Agent: curl/7.59.0
Accept: */*
Content-Length: 172
Content-Type: multipart/form-data; boundary=------------------------2e4ba7fc50e9eec8

--------------------------2e4ba7fc50e9eec8
Content-Disposition: form-data; name="file"

<html><body>Inline!</body></html>
--------------------------2e4ba7fc50e9eec8--
hanshenrik
  • 19,904
  • 4
  • 43
  • 89
  • I think that your answer is much simpler and suitable as the answer for OP's issue. I could study from your answer. Thank you. And I'm really sorry for my ignorant answer. Although I cannot accept, I would like to upvote. – Tanaike May 27 '18 at 22:31
0

How about this answer? I think that there might be several solutions. So please think of this as one of them. In order to send the content of a.html with multipart/form-data, the request body is required to be created and send it. As a example, this answer supposes as follows.

  • a.html is <html><body>Inline!</body></html>.
  • You use curl on bash.

The request body is as follows.

--boundaryboundary
Content-Disposition: form-data; name="file"; filename="a.html"
Content-Type: text/html

<html><body>Inline!</body></html>
--boundaryboundary--

Replace the line break to \r\n and send this.

--boundaryboundary\r\nContent-Disposition: form-data; name="file"; filename="a.html"\r\nContent-Type: text/html\r\n\r\n<html><body>Inline!</body></html>\r\n--boundaryboundary--

Modified curl sample :

The modified curl sample is as follows. Content-Type is multipart/form-data; boundary=boundaryboundary.

curl -X POST \
  -H 'Content-Type: multipart/form-data; boundary=boundaryboundary' \
  -d $'--boundaryboundary\r\nContent-Disposition: form-data; name="file"; filename="a.html"\r\nContent-Type: text/html\r\n\r\n<html><body>Inline!</body></html>\r\n--boundaryboundary--' \
  'http://localhost:8080/convert'

Reference :

If this was not what you want, I'm sorry.

Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • Excellent. Yes this works, and seems fairly straightforward to work with. I was hoping to not have to manually construct the form data but in fact it's simple enough so long as you know the rules for line breaks and boundaries – Chris F Carroll May 27 '18 at 13:03
  • @Chris F Carroll Thank you for replying. And I have to apologize to you. Now I noticed another answer. I think that hanshenrik's answer is much simpler and suitable as the answer for your issue. So please accept hanshenrik's answer. And I'm really sorry for my ignorant answer. – Tanaike May 27 '18 at 22:31
  • I agree that form-string is the the simpler thing, but since your answer both works and demonstrates other things too, it is still a valuable answer. – Chris F Carroll May 28 '18 at 15:31
  • @Chris F Carroll Thank you for your response and your concern. – Tanaike May 28 '18 at 21:43