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.

 Answers

4

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".

Reference: http://php.net/manual/en/wrappers.php.php

Tuesday, December 20, 2022
1

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

x.send('name1='+string+'&name2=value2'); 

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
 
2

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'));
//etc...
$parallel_curl->finishAllRequests();
$collector->outputData();
Tuesday, August 30, 2022
 
4

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.

Sources:

http://grokbase.com/t/gg/golang-nuts/12adq8a2ys/go-nuts-re-reading-http-response-body-or-any-reader

https://medium.com/@xoen/golang-read-from-an-io-readwriter-without-loosing-its-content-2c6911805361

Saturday, December 3, 2022
3
  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 e.name = ?");
    }
    getEntityManager().createQuery(builder.toString());
    
  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 :