简体   繁体   中英

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. To accept or reject the invitation, the other user needs to do an empty POST to:

/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:

Request entity too large!

The POST method does not allow the data transmitted, or the data volume exceeds the capacity limit.

Obviously this is not true since the POST data is empty (I double-checked in Firebug). So my questions are:

  • Does the HTTP protocol allow POSTing empty data? 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?

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.

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

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

Why are you doing a POST request? Make your API detect if data is sent or not and if there is no data, then make a GET request. 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).

There's no point making a POST request in your case:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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