You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Go语言中发送二进制压缩的multipart/form-data请求的疑问

Sending Compressed Binary Data with Your POST Request

Let's tackle this step by step—you already have your request headers set up, now we just need to adjust the curl command to send that compressed binary data. There are two common scenarios here, depending on whether you're sending the entire request body as compressed data or including it as part of a multipart form.

Scenario 1: Send the Entire Request Body as Gzip-Compressed Binary Data

If your POST payload is entirely a gzipped binary file, here's what you need to do:

  1. First, make sure your data is actually compressed (use gzip -c your-original-data.bin > compressed-data.gz if you haven't already).
  2. Add the Content-Encoding: gzip header—this tells the server it needs to decompress the request body before processing it.
  3. Use --data-binary @your-file to send the raw binary content of your compressed file.

Here's the updated curl command:

curl 'http://192.168.1.90:333/sendMessage.srv' \
  -H 'Origin: http://192.168.1.90:333' \
  -H 'Accept-Encoding: gzip, deflate' \
  -H 'Accept-Language: en-US,en;q=0.9' \
  -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36' \
  -H 'Content-Encoding: gzip' \
  -H 'Content-Type: application/octet-stream' \
  --data-binary @compressed-data.gz

Adjust the Content-Type to match the original data type (e.g., application/json if the uncompressed data was JSON) if the server expects it—application/octet-stream works for generic binary data.

Scenario 2: Include Compressed Binary Data in a Multipart Form

If you're using multipart/form-data (like your original command hints at), you can upload the compressed file as a form field with the -F flag. You can also specify metadata like the file's MIME type and encoding:

Basic file upload in form:

curl 'http://192.168.1.90:333/sendMessage.srv' \
  -H 'Origin: http://192.168.1.90:333' \
  -H 'Accept-Encoding: gzip, deflate' \
  -H 'Accept-Language: en-US,en;q=0.9' \
  -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36' \
  -F "attachment=@compressed-data.gz;type=application/gzip" \
  -F "message=Hello, this is a compressed file"

Here, attachment is the form field name, @compressed-data.gz points to your file, and type=application/gzip tells the server what kind of file it's receiving.

If you need to explicitly signal the content is compressed:

If the server expects a Content-Encoding header for the specific form field, you can add it like this:

-F "attachment=@compressed-data.gz;type=application/octet-stream;headers=\"Content-Encoding: gzip\""

Bonus: Compress Data on the Fly

If you don't want to pre-save a compressed file, you can pipe uncompressed data through gzip and send it directly:

cat your-uncompressed-data.bin | gzip | curl 'http://192.168.1.90:333/sendMessage.srv' \
  -H 'Origin: http://192.168.1.90:333' \
  -H 'Accept-Encoding: gzip, deflate' \
  -H 'Accept-Language: en-US,en;q=0.9' \
  -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36' \
  -H 'Content-Encoding: gzip' \
  -H 'Content-Type: application/octet-stream' \
  --data-binary @-

The @- tells curl to read the request body from standard input (the output of the gzip command).

Quick Notes:

  • Remember: Accept-Encoding is for telling the server you accept compressed responses—it doesn't affect your outgoing compressed data. The key header here is Content-Encoding: gzip.
  • Double-check that your server is configured to handle compressed request bodies—some servers don't support this by default, so you might need to adjust backend settings if you get errors.

内容的提问来源于stack exchange,提问作者Peter S

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 08:57:27