-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bug: Formatter disallows string data with application/json content-type. #8397
Comments
Thank you for reporting. @codeigniter4/core-team This seems to be a bug. |
Yes, and a fix sounds like a BC break 😜 Although we had a similar conversation not so long ago when it comes to incoming JSON data. And we decided to not change anything if I remember correctly. |
I think we should follow the standard. At least Enable Auto Routing Improved. <?php
namespace App\Controllers;
class Test extends BaseController
{
public function postPost()
{
$json = $this->request->getJSON();
return $this->response->setContentType('application/json')
->setBody(json_encode($json));
}
} $ curl -H 'Content-Type: application/json' -d '100' localhost:8080/test/post
100 $ curl -H 'Content-Type: application/json' -d '"message"' localhost:8080/test/post
"message" $ curl -H 'Content-Type: application/json' -d '{"foo": "bar"}' localhost:8080/test/post
{"foo":"bar"} |
@trampgeek I sent a PR #8490 |
Many thanks for dealing with this, kenjis. Looks great. :-) |
@trampgeek If you believe the PR should be merged, approve it. |
Ah, sorry. I wasn't familiar with the process. Thanks for clarifying. Have approved. |
Closed #8490 |
PHP Version
8.1
CodeIgniter4 Version
4.4.4
CodeIgniter4 Installation Method
Manual (zip or tar.gz)
Which operating systems have you tested for this bug?
Linux
Which server did you use?
apache
Database
N/A
What happened?
In a simple REST server controller, I call
$this->respond('Error message', 200)
.The content type of the request is
application/json
and the response should be of the same type. But the format function in ResponseTrait.php considers passing a string as the$data
parameter to be an error; it does not json_encode the string and changes the content type totext/html
.This seems wrong to me. As I read the spec for the application/json Mime type (see RFC4627 and RFC8259, the only requirement is that the response body be valid JSON, which includes integers, strings and bools, not just JSON objects. Thus the value of string in the response should have been json_encoded (which adds literal double-quote characters to each end) and the content type should have been left unchanged.
Steps to Reproduce
Have a RESTful resource controller like:
Route POST requests to the post method and send an HTTP POST request to the app, e.g. with a curl command like
The app returns with a content type of
text/html
and the response body is justThe response should be
application/json
and the response body should be(where the double quotes should be present in the response).
Expected Output
See above.
Anything else?
No response
The text was updated successfully, but these errors were encountered: