简体   繁体   English

是否允许发布空数据?

[英]Is it allowed to POST empty data?

For a web API I'm developing I want to do something similar to Facebook Graph API. A user can invite another user.对于我正在开发的 web API 我想做类似于 Facebook 图 API 的事情。一个用户可以邀请另一个用户。 To accept or reject the invitation, the other user needs to do an empty POST to:要接受或拒绝邀请,其他用户需要执行一个空的 POST 到:

/invitation/<invitationId>/accepted

or要么

/invitation/<invitationId>/rejected

However, this is not working for me as, when I POST empty data, PHP (or Apache?) returns the following error:但是,这对我不起作用,因为当我发布空数据时,PHP(或 Apache?)返回以下错误:

Request entity too large!请求的实体太大!

The POST method does not allow the data transmitted, or the data volume exceeds the capacity limit. POST方式不允许传输数据,或数据量超过容量限制。

Obviously this is not true since the POST data is empty (I double-checked in Firebug).显然这不是真的,因为 POST 数据是空的(我在 Firebug 中仔细检查过)。 So my questions are:所以我的问题是:

  • Does the HTTP protocol allow POSTing empty data? HTTP 协议是否允许发布空数据? If it doesn't I guess I'll just post some dummy data but I'd rather avoid this kind of hack.如果没有,我想我会发布一些虚拟数据,但我宁愿避免这种黑客行为。

  • If it is allowed, how can I make Apache/PHP allow the request?如果允许,我怎样才能让 Apache/PHP 允许请求?

I guess that you want to do some kind of a RESTful api, and that's why you need to send post with no data.我猜您想做某种 RESTful api,这就是为什么您需要发送没有数据的帖子。

Apache/php should allow you to send empty post data, the restrictions must be imposed by some server configurations of the framework you are using Apache/php 应该允许你发送空的 post 数据,限制必须由你正在使用的框架的一些服务器配置强加

you can test this with this simple file:你可以用这个简单的文件来测试它:

<?php

echo '<h2>This page was requested using ' . $_SERVER['REQUEST_METHOD'] . '</h2>';
echo '<pre>';
print_r($_POST);
echo '</pre>';
?>
<form action="" method="post">
    <input type="submit" value="Request this page using POST" />
</form>

You will see that the script detects the post request (using REQUEST_METHOD from the _SERVER superglobal) even if the _POST array is empy您将看到脚本检测到发布请求(使用来自 _SERVER 超全局的 REQUEST_METHOD),即使 _POST 数组是空的

Why are you doing a POST request?你为什么要发出 POST 请求? Make your API detect if data is sent or not and if there is no data, then make a GET request.让你的 API 检测是否有数据发送,如果没有数据,则发出 GET 请求。 In fact, make a GET request in most cases as it is far more efficient, since browser can cache that information more easily (and you can make your API smart to take advantage of that by sending cache-specific headers).事实上,在大多数情况下,发出 GET 请求效率更高,因为浏览器可以更轻松地缓存该信息(并且您可以通过发送特定于缓存的标头使您的 API 智能地利用它)。

There's no point making a POST request in your case:)在你的情况下发出 POST 请求是没有意义的:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM