I'm using a docker setup with php 8.0.15 + phalcon 5.0.0beta2 and i'm trying to do a simple post request using the fetch api to a route.
/* inside router.php */
$apiGroup = new Router\Group();
$apiGroup->setPrefix('/api');
$apiGroup->addPost('/user', 'UserApi::post');
/* somewhere in my controller action */
$data = [
'email' => $this->request->getPost('email', [ Filter::FILTER_EMAIL ]),
'password' => $this->request->getPost('password', [ Filter::FILTER_STRING ]),
];
/* in my js */
fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(someData)
}).then(dostuff);
My problem is that$this->request->getPost('email')
returns null and when debugging I saw that $_POST is also empty. Using$this->request->getRawBody()
and$this->request->getJsonRawBody()
do yield results since my data is actually there. I could very well just usegetJsonRawBody()
, but i'm wondering why the behaviour? (I used phalcon 3.* for another project and it worked just fine)
Thanks.
The documentation for Phalcon's Request object explains that it is primarily a wrapper around PHP's native "superglobals", such as$_POST
. In particular:
The $_POST superglobal contains an associative array that contains the variables passed to the current script via the HTTP POST method when using
application/x-www-form-urlencoded
ormultipart/form-data
as the HTTPContent-Type
in the request. You can retrieve the data stored in the array by calling thegetPost()
method
Note that important caveat that this array is only populated when using two specific content types - specifically, the two content types that browsers use when submitting HTML forms. Form processing is the original task PHP was built for, and it can handle things like file uploads; but it doesn't natively do anything with JSON, XML, or other input types.
(As an aside, that documentation is subtly wrong: despite its name,$_POST
will be populated for any HTTP method with an appropriate request body, e.g.PUT
, just as$_GET
is populated even when the method isPOST
.)
In your request, you're not using those content types, because you're posting a JSON string, so PHP doesn't populate$_POST
, and Phalcon doesn't have anything to read withgetPost()
.
As you've discovered, there's a separate method which allows you to decode the JSON body to a PHP array:
getJsonRawBody(): Gets decoded JSON HTTP raw request body
There's no mention of JSON at all in the Phalcon 3.4 Request documentation so I'm not sure whether that functionality has changed, you've misremembered, or you were using some additional plugin or configuration.
The REST tutorial shows usage ofgetJsonRawBody()
in both the 5.0 version and the 3.4 version.
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Phalcon is a special PHP framework, the source code of which is written in C. It may seem strange, but thanks to this approach, this framework can be called the fastest project of its kind. Working with this framework does not mean you need to have C development experience. A programmer choosing Phalcon can only deal with PHP classes and namespaces generated by the framework.
https://phalcon.io/en-us
HTML (English "hyper text markup language" - hypertext markup language) is a special markup language that is used to create sites on the Internet.
Browsers understand html perfectly and can interpret it in an understandable way. In general, any page on the site is html-code, which the browser translates into a user-friendly form. By the way, the code of any page is available to everyone.
https://www.w3.org/html/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.