将 multipart/form-data 请求调整为在同一请求中包含图像和 json 的curl

发布于 2025-01-15 17:24:09 字数 787 浏览 2 评论 0原文

我需要重写从浏览器中提取的这个curl请求以从bash执行它,它是一个multipart/form-data,它将图像的上传与同一请求中的json结合在一起,我已经进行了几次测试来替换原始数据两个 -F 的数据,但服务器响应错误。 正确的方法是什么? 我也无法理解 WebKitFormBoundary 旁边的键是一个看起来随机的值,它只是一个分隔符吗?我可以使用任何东西吗?

我删除了我认为不相关的标题,

curl 'https://localhost' \
  -H 'Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryXXXXXXXXXXXXXXXX' 
  -H 'Accept: */*' \
  --data-raw $'------WebKitFormBoundaryXXXXXXXXXXXXXXXX\r\nContent-Disposition: form-data; name="image"; filename="image.jpg"\r\nContent-Type: image/jpeg\r\n\r\n\r\n------WebKitFormBoundaryXXXXXXXXXXXXXXXX\r\nContent-Disposition: form-data; name="item"; filename="blob"\r\nContent-Type: application/json\r\n\r\n{"item1":"xxxx","item2":"yyyy"}\r\n------WebKitFormBoundaryXXXXXXXXXXXXXXXX--\r\n' \

谢谢

I need to rewrite this curl request extracted from the browser to execute it from bash, it is a multipart/form-data that combines the upload of an image together with a json in the same request, I have carried out several tests substituting the raw data for two -F but the server responds with error.
What would be the correct way?
I also fail to understand the key that is next to WebKitFormBoundary is a value that seems random, is it just a separator? can i use anything?

I have removed the headers that I considered irrelevant

curl 'https://localhost' \
  -H 'Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryXXXXXXXXXXXXXXXX' 
  -H 'Accept: */*' \
  --data-raw 

Thanks

------WebKitFormBoundaryXXXXXXXXXXXXXXXX\r\nContent-Disposition: form-data; name="image"; filename="image.jpg"\r\nContent-Type: image/jpeg\r\n\r\n\r\n------WebKitFormBoundaryXXXXXXXXXXXXXXXX\r\nContent-Disposition: form-data; name="item"; filename="blob"\r\nContent-Type: application/json\r\n\r\n{"item1":"xxxx","item2":"yyyy"}\r\n------WebKitFormBoundaryXXXXXXXXXXXXXXXX--\r\n' \

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

や三分注定 2025-01-22 17:24:09

我也无法理解 WebKitFormBoundary 旁边的键是一个看起来随机的值,它只是一个分隔符吗?我可以使用任何东西吗?

是的,是的,实际上说是的。它应该是一个随机保证的唯一字符串,用作分隔符来表示表单的开始和结束。如果您事先知道上传的内容都不包含字符串 stackoverflow,您可以使用 内容类型:多部分/表单数据;例如,boundary=stackoverflow(但如果您上传实际包含字符串 stackoverflow 的文件,这可能会中断!),但您应该让curl 自动生成该字符串,而不是自己指定它, curl 将自动生成一个实际上保证唯一的字符串。

我删除了我认为不相关的标题

...如果您猜错了,服务器可能会因为缺少必需的标头而给您一个错误。假设您在删除不相关标头时没有犯任何错误,但我认为

curl 'https://localhost'\
  --form "[email protected];type=image/jpeg"\
  --form "item=@blob;type=application/json"

该命令生成的请求是错误的:

POST / HTTP/1.1
Host: localhost
User-Agent: curl/7.81.0
Accept: */*
Content-Length: 359
Content-Type: multipart/form-data; boundary=------------------------7d0262831ce8f248

--------------------------7d0262831ce8f248
Content-Disposition: form-data; name="image"; filename="image.jpg"
Content-Type: image/jpeg

image.jpg content

--------------------------7d0262831ce8f248
Content-Disposition: form-data; name="item"; filename="blob"
Content-Type: application/json

["json lol"]

--------------------------7d0262831ce8f248--

如您在上面看到的,接受:*/* 无论如何都是curl默认的accept-header,所以不需要指定它,curl会自动生成边界------------------------ --7d0262831ce8f248所以也无需手动指定边界。

I also fail to understand the key that is next to WebKitFormBoundary is a value that seems random, is it just a separator? can i use anything?

yes and yes and practically speaking yes. it's supposed to be a random-guaranteed-unique string used as separator to signal start and end of forms.. if you know beforehand that none of your uploads contains the string stackoverflow, you could use Content-Type: multipart/form-data; boundary=stackoverflow for example (but this may break if you upload a file actually containing the string stackoverflow !), but you should let curl generate that string automatically, rather than specifying it yourself, curl will auto-generate a string that is practically guaranteed to be unique.

I have removed the headers that I considered irrelevant

... if you guessed wrong, the server may give you an error for missing a required header. assuimg that you didn't make any mistakes in removing irrelevant headers though, i think it's

curl 'https://localhost'\
  --form "[email protected];type=image/jpeg"\
  --form "item=@blob;type=application/json"

the request generated by that command is roghly:

POST / HTTP/1.1
Host: localhost
User-Agent: curl/7.81.0
Accept: */*
Content-Length: 359
Content-Type: multipart/form-data; boundary=------------------------7d0262831ce8f248

--------------------------7d0262831ce8f248
Content-Disposition: form-data; name="image"; filename="image.jpg"
Content-Type: image/jpeg

image.jpg content

--------------------------7d0262831ce8f248
Content-Disposition: form-data; name="item"; filename="blob"
Content-Type: application/json

["json lol"]

--------------------------7d0262831ce8f248--

as you can see above, Accept: */* is curl's default accept-header anyway, so there's no need to specify it, and curl auto-generated the boundary ------------------------7d0262831ce8f248 so there's no need to manually specify a boundary either.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文