在 curl 中传递 POST 数据时请求实体太大
Request entity too large when passing POST data in curl
我有一个 fastcgi++ 服务。它通过 POST 需要 4 个参数,因为其中一个是密码。我的样本输入是 61 个字符。这就是我调用服务的方式:
curl --data 'name=test&address=abc&phoneNumber=0987654321&password=test123' http://localhost/cgi-bin/add-user.fcg
我收到此错误:413 请求实体太大。根据我的研究,我发现这意味着 apache 期望的主体比它得到的要小。所以,我在httpd.conf中添加了LimitRequestBody 0
(我不是在生产环境中)。但是apache还是报错请求实体太大
我在 curl 中添加了 -v,这是输出:
* Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 80 (#0)
> POST /cgi-bin/add-user.fcg HTTP/1.1
> Host: localhost
> User-Agent: curl/7.61.1
> Accept: */*
> Content-Length: 60
> Content-Type: application/x-www-form-urlencoded
>
* upload completely sent off: 60 out of 60 bytes
< HTTP/1.1 413 Request Entity Too Large
< Date: Sun, 24 Mar 2019 03:48:09 GMT
< Server: Apache/2.4.38 (Fedora) mod_fcgid/2.3.9
< Connection: close
< Transfer-Encoding: chunked
< Content-Type: text/html; charset=utf-8
<
* Closing connection 0
<!DOCTYPE html><html lang='en'><head><title>413 Request Entity Too Large</title></head><body><h1>413 Request Entity Too Large</h1></body></html>
这是我的 httpd.conf:
ServerRoot "/etc/httpd"
Listen 80
Include conf.modules.d/*.conf
User apache
Group apache
ServerAdmin root@localhost
<Directory />
AllowOverride none
Require all denied
</Directory>
DocumentRoot "/var/www/html"
<Directory "/var/www">
AllowOverride None
# Allow open access:
Require all granted
</Directory>
<Directory "/var/www/html">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
<IfModule dir_module>
DirectoryIndex index.html
</IfModule>
<Files ".ht*">
Require all denied
</Files>
ErrorLog "logs/error_log"
LogLevel warn
<IfModule log_config_module>
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %b" common
<IfModule logio_module>
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
</IfModule>
CustomLog "logs/access_log" combined
</IfModule>
<IfModule alias_module>
ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
</IfModule>
<Directory "/var/www/cgi-bin">
AllowOverride None
Options +ExecCGI
Require all granted
SetHandler fcgid-script
</Directory>
<IfModule mime_module>
TypesConfig /etc/mime.types
AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz
AddType text/html .shtml
AddOutputFilter INCLUDES .shtml
</IfModule>
AddDefaultCharset UTF-8
<IfModule mime_magic_module>
MIMEMagicFile conf/magic
</IfModule>
EnableSendfile on
IncludeOptional conf.d/*.conf
#I added the line below
LimitRequestBody 0
这是 fcgid.conf:
AddHandler fcgid-script fcg fcgi fpl
Sane place to put sockets and shared memory file
FcgidIPCDir /run/mod_fcgid
FcgidProcessTableFile /run/mod_fcgid/fcgid_shm
(不是答案,但对于评论来说太大了)
如果你 运行 这个 php 脚本你会得到什么?
<?php
declare(strict_types=1);
header("Content-Type: text/plain;charset=utf-8");
$ch=curl_init('http://localhost/cgi-bin/add-user.fcg');
curl_setopt_array($ch,array(CURLOPT_USERAGENT=>'curl/7.61.1',CURLOPT_RETURNTRANSFER=>1));
$post_str="";
for($i=0;$i<61;++$i){
curl_setopt_array($ch,array(
CURLOPT_POST=>1,
CURLOPT_POSTFIELDS=>$post_str,
));
curl_exec($ch);
$code=curl_getinfo($ch,CURLINFO_RESPONSE_CODE);
echo "bytes: {$i} code: {$code}\n";
$post_str.="a";
}
curl_close($ch);
echo "finished loop";
我应该提到我正在使用 fastcgi++ 库。由于@covener mentioned, the 413 page was not apache's. It turns out that it was fastcgi++. I should have read the docs first. It clearly mentions here默认情况下不启用POST。将构造函数添加到我的 class 并使用我想要的 POST 请求的最大大小调用 Request 构造函数,解决了问题。
P.S。非常感谢@hansherik 为我所做的所有调试时间。
我有一个 fastcgi++ 服务。它通过 POST 需要 4 个参数,因为其中一个是密码。我的样本输入是 61 个字符。这就是我调用服务的方式:
curl --data 'name=test&address=abc&phoneNumber=0987654321&password=test123' http://localhost/cgi-bin/add-user.fcg
我收到此错误:413 请求实体太大。根据我的研究,我发现这意味着 apache 期望的主体比它得到的要小。所以,我在httpd.conf中添加了LimitRequestBody 0
(我不是在生产环境中)。但是apache还是报错请求实体太大
我在 curl 中添加了 -v,这是输出:
* Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 80 (#0)
> POST /cgi-bin/add-user.fcg HTTP/1.1
> Host: localhost
> User-Agent: curl/7.61.1
> Accept: */*
> Content-Length: 60
> Content-Type: application/x-www-form-urlencoded
>
* upload completely sent off: 60 out of 60 bytes
< HTTP/1.1 413 Request Entity Too Large
< Date: Sun, 24 Mar 2019 03:48:09 GMT
< Server: Apache/2.4.38 (Fedora) mod_fcgid/2.3.9
< Connection: close
< Transfer-Encoding: chunked
< Content-Type: text/html; charset=utf-8
<
* Closing connection 0
<!DOCTYPE html><html lang='en'><head><title>413 Request Entity Too Large</title></head><body><h1>413 Request Entity Too Large</h1></body></html>
这是我的 httpd.conf:
ServerRoot "/etc/httpd"
Listen 80
Include conf.modules.d/*.conf
User apache
Group apache
ServerAdmin root@localhost
<Directory />
AllowOverride none
Require all denied
</Directory>
DocumentRoot "/var/www/html"
<Directory "/var/www">
AllowOverride None
# Allow open access:
Require all granted
</Directory>
<Directory "/var/www/html">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
<IfModule dir_module>
DirectoryIndex index.html
</IfModule>
<Files ".ht*">
Require all denied
</Files>
ErrorLog "logs/error_log"
LogLevel warn
<IfModule log_config_module>
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %b" common
<IfModule logio_module>
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
</IfModule>
CustomLog "logs/access_log" combined
</IfModule>
<IfModule alias_module>
ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
</IfModule>
<Directory "/var/www/cgi-bin">
AllowOverride None
Options +ExecCGI
Require all granted
SetHandler fcgid-script
</Directory>
<IfModule mime_module>
TypesConfig /etc/mime.types
AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz
AddType text/html .shtml
AddOutputFilter INCLUDES .shtml
</IfModule>
AddDefaultCharset UTF-8
<IfModule mime_magic_module>
MIMEMagicFile conf/magic
</IfModule>
EnableSendfile on
IncludeOptional conf.d/*.conf
#I added the line below
LimitRequestBody 0
这是 fcgid.conf:
AddHandler fcgid-script fcg fcgi fpl
Sane place to put sockets and shared memory file
FcgidIPCDir /run/mod_fcgid
FcgidProcessTableFile /run/mod_fcgid/fcgid_shm
(不是答案,但对于评论来说太大了)
如果你 运行 这个 php 脚本你会得到什么?
<?php
declare(strict_types=1);
header("Content-Type: text/plain;charset=utf-8");
$ch=curl_init('http://localhost/cgi-bin/add-user.fcg');
curl_setopt_array($ch,array(CURLOPT_USERAGENT=>'curl/7.61.1',CURLOPT_RETURNTRANSFER=>1));
$post_str="";
for($i=0;$i<61;++$i){
curl_setopt_array($ch,array(
CURLOPT_POST=>1,
CURLOPT_POSTFIELDS=>$post_str,
));
curl_exec($ch);
$code=curl_getinfo($ch,CURLINFO_RESPONSE_CODE);
echo "bytes: {$i} code: {$code}\n";
$post_str.="a";
}
curl_close($ch);
echo "finished loop";
我应该提到我正在使用 fastcgi++ 库。由于@covener mentioned, the 413 page was not apache's. It turns out that it was fastcgi++. I should have read the docs first. It clearly mentions here默认情况下不启用POST。将构造函数添加到我的 class 并使用我想要的 POST 请求的最大大小调用 Request 构造函数,解决了问题。
P.S。非常感谢@hansherik 为我所做的所有调试时间。