Post-Data empty


#1

Hey folks!

since an update (I think), I have a problem receiving post-data. $this->request->data or $this->request->getData() is empty. No matter, where I check this data. I checked it in “AppController initialize and beforeFilter” and also in “…Controller initialize and beforeFilter”. Maybe, I am accessing it in a wrong way, because the data gets saved. It is a bit strange, because accessing postdata worked all the time.

When I debug $this->request->data and $this->request->getData()" and$this->requestand$this`, I could never find my data what I posted. The array “data” is empty in request.

Originally, I wanted to check if “created” and “modified” are set via postData. If so, I want to unset/delete these values, so that cakephp handles it hisself via timestamp behavior. This only applies for the REST-Api I created.

Here is my corresponding code.

//AppController (API)
public function beforeFilter(Event $event) {                              
    
    $this->Auth->allow(['token']);
    
    if(array_key_exists("created", $this->request->data)) {
        if ( (substr($this->request->data["created"], 0, 1) == "0") || ($this->request->data["created"] == "null") ) {
            unset($this->request->data["created"]);
        }
    }
    if(array_key_exists("modified", $this->request->data)) {
        if ( (substr($this->request->data["modified"], 0, 1) == "0") || ($this->request->data["modified"] == "null") ) {
            unset($this->request->data["modified"]);
        }
    }
    debug($this->request->data);
}

Thanks in advance!


#2

Hello,

I just found out, that this is an Content-Type-Based error. I made a request against the api with these headers:

“Accept: application/json”
"Content-Type: application/json"
Body-data is {“addressbook_id”: 8}

This worked well all the time. But now $this->request->data is empty that way. When I change the content-type, data is filled but it doesn’t deserialize/handle the json-params.

I think this is due to cake 3.4.

What do I have to do?
Thanks!


#3

So, got some news… I figured out, that I have to use input() like that

$requestJsonData = $this->request->input("json_decode", true);

In that way, I receive the postData as an array. So I am able to modify it in beforeFilter().

Now I want to set it back using withBody(). But to be honest, I don’t get it to work. Can somebody help me regarding writing back this data?

And furthermore: Is this the right approach?

Thanks.


#4

Okay, I finally came to a solution.

I didn’t modify the data at the request body, I made a global event (Model.beforeSave) and assigned it in AppController->initialize to the global event manager.

To this event the entity is passed. So I can check and modify the fields there directly on the entity.

I was on SlackChat with @dereuromark and @ADmad, they said, one should not alter the request body. So I thought of another solution, which then resulted in making a global event.

Does this sound better? Can somebody please give me a feedback on this?