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

如何在curl命令行中设置Content-Length请求头?

解决curl PUT请求返回411 Length Required的问题

你碰到的HTTP/1.1 411 Length Required错误,本质是服务器要求PUT请求必须携带Content-Length头——用来明确告知服务器请求体的字节长度,哪怕你没有要发送的请求体内容。

一、这个场景下的Content-Length值是什么?

分两种核心情况:

  • 如果PUT请求没有实际请求体:此时Content-Length应该设为0,明确告诉服务器“我没有要提交的数据”。从你给出的curl命令来看,没有携带请求体参数(比如-d),所以大概率属于这种情况。
  • 如果请求有application/x-www-form-urlencoded格式的请求体:比如你要提交表单数据key1=val1&key2=val2,那Content-Length的值就是这段表单字符串的字节数(注意编码:默认UTF-8下,普通字符占1字节,特殊字符可能多字节)。

二、怎么用curl设置Content-Length?

有两种实用方式:

  1. 手动指定请求头:直接通过-H参数添加Content-Length。针对空请求体的场景,命令如下:
    curl -XPUT "https://example.org?p1=value1&p2=value2" -H"Content-Type:application/x-www-form-urlencoded" -H"Content-Length: 0"
    
    如果有请求体,比如要发送data=test(字节数为7),可以这样写:
    curl -XPUT "https://example.org?p1=value1&p2=value2" -H"Content-Type:application/x-www-form-urlencoded" -H"Content-Length: 7" -d "data=test"
    
  2. 让curl自动计算:当你使用-d(或--data)参数发送请求体时,curl会自动计算请求体的字节数,并帮你添加正确的Content-Length头,无需手动设置。比如:
    curl -XPUT "https://example.org?p1=value1&p2=value2" -H"Content-Type:application/x-www-form-urlencoded" -d "username=foo&password=bar"
    

需要注意:如果服务器严格校验Content-Length,但你确实没有请求体,一定要手动加上-H"Content-Length: 0"——不然curl可能不会自动发送这个头,依旧会触发411错误。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 06:22:12