Viewed   122 times

file_get_contents("php://input") or $HTTP_RAW_POST_DATA - which one is better to get the body of JSON request?

And which request type (GET or POST) should I use to send JSON data when using client side XmlHTTPRequest?

My question was inspired from this answer: How to post JSON to PHP with curl

Quote from that answer:

From a protocol perspective file_get_contents("php://input") is actually more correct, since you're not really processing http multipart form data anyway.



Actually php://input allows you to read raw POST data.

It is a less memory intensive alternative to $HTTP_RAW_POST_DATA and does not need any special php.ini directives.

php://input is not available with enctype="multipart/form-data".


Tuesday, December 20, 2022

You forgot to name your variables in the send function. The good way to use it is


Given that, I think you will have to change the content-length header. I don't think it is usefull to send it.

One another thing you can do is try with GET method. You can also try to change your content-type header by that one :

xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
Wednesday, August 3, 2022

Hm, I am NOT a fan or parallel curl request (why tie up the webserver, stuff like GearMan is better suited), but if you are going this route:

class ReturnCollector {
   private $data = array();
   function addData($content){
      $this->data[] = json_decode($content, true);
   function getData(){
      return $this->data);
   function outputData(){
      echo json_encode($this->getData());

$collector = new ReturnCollector();
$parallel_curl->startRequest($url1, array($collector,'addData'));
$parallel_curl->startRequest($url2, array($collector,'addData'));
$parallel_curl->startRequest($url3, array($collector,'addData'));
Tuesday, August 30, 2022

TIL that http.Response.Body is a buffer, which means that once it has been read, it cannot be read again.

It's like a stream of water, you can see it and measure it as it passes but once it's gone, it's gone.

However, knowing this, there is a workaround, you need to "catch" the body and restore it:

// Read the Body content
var bodyBytes []byte
if context.Request().Body != nil {
    bodyBytes, _ = ioutil.ReadAll(context.Request().Body)

// Restore the io.ReadCloser to its original state
context.Request().Body = ioutil.NopCloser(bytes.NewBuffer(bodyBytes))

// Continue to use the Body, like Binding it to a struct:
order := new(models.GeaOrder)
error := context.Bind(order)

Now, you can use context.Request().Body somewhere else.


Saturday, December 3, 2022
  1. createQuery()

    It should be used for dynamic query creation.

    //Example dynamic query
    StringBuilder builder = new StringBuilder("select e from Employee e");
    if (empName != null) {
        builder.append(" where = ?");
  2. createNamedQuery()

    It is like a constant variable which can be reused by name. You should use it in common database calls, such as "find all users", "find by id", etc.

  3. createNativeQuery()

    This creates a query that depends completely on the underlying database's SQL scripting language support. It is useful when a complex query is required and the JPQL syntax does not support it.

    However, it can impact your application and require more work, if the underlying database is changed from one to another. An example case would be, if your development environment is in MySQL, and your production environment is using Oracle. Plus, the returned result binding can be complex if there is more than a single result.

Wednesday, August 17, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :