curl:在名为file的多部分表单中发布字段?

时间:2018-05-26 20:03:58

标签: curl

我想替换它:

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

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

但这只是让我:curl: (26) couldn't open file "html><body>a</body></html>"

使用curl -d不起作用(大概是因为它没有生成多部分体?)

如何在curl参数中嵌入内容,而不是依赖于对实际文件的引用?

2 个答案:

答案 0 :(得分:2)

使用--form-string参数而不是-F,例如

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

产生

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--

答案 1 :(得分:0)

这个答案怎么样?我认为可能有几种解决方案。所以请把它想象成其中之一。为了使用multipart / form-data发送a.html的内容,需要创建请求正文并将其发送。例如,这个答案假设如下。

  • a.html<html><body>Inline!</body></html>
  • 你在bash上使用curl。

请求正文如下。

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

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

将换行符替换为\r\n并发送。

--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--

修改后的卷曲样本:

修改的卷曲样品如下。 Content-Typemultipart/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'

参考:

如果这不是你想要的,我很抱歉。

相关问题