Skip to content
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

Closed
trampgeek opened this issue Jan 3, 2024 · 8 comments
Closed
Labels
bug Verified issues on the current code behavior or pull requests that will fix them

Comments

@trampgeek
Copy link

trampgeek commented Jan 3, 2024

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 to text/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:

    namespace App\Controllers;

    use CodeIgniter\RESTful\ResourceController;
    use CodeIgniter\API\ResponseTrait;

    class Things extends ResourceController
    {
           public function post()
           {
                   return $this->respond('Error message', 200);
            }
    }

Route POST requests to the post method and send an HTTP POST request to the app, e.g. with a curl command like

curl -H 'Content-Type: application/json' -d '{"x": "blah"}'  localhost:8080/things -v

The app returns with a content type of text/html and the response body is just

Error message

The response should be application/json and the response body should be

"Error message"

(where the double quotes should be present in the response).

Expected Output

See above.

Anything else?

No response

@trampgeek trampgeek added the bug Verified issues on the current code behavior or pull requests that will fix them label Jan 3, 2024
@kenjis
Copy link
Member

kenjis commented Jan 7, 2024

Thank you for reporting.

@codeigniter4/core-team This seems to be a bug.

@michalsn
Copy link
Member

michalsn commented Jan 7, 2024

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.

@kenjis
Copy link
Member

kenjis commented Jan 26, 2024

I think we should follow the standard.

At least getJSON() can get all valid JSON data.

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

@kenjis
Copy link
Member

kenjis commented Jan 31, 2024

@trampgeek I sent a PR #8490

@trampgeek
Copy link
Author

Many thanks for dealing with this, kenjis. Looks great. :-)

@kenjis
Copy link
Member

kenjis commented Feb 1, 2024

@trampgeek
Copy link
Author

Ah, sorry. I wasn't familiar with the process. Thanks for clarifying. Have approved.

@kenjis
Copy link
Member

kenjis commented Feb 4, 2024

Closed #8490

@kenjis kenjis closed this as completed Feb 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Verified issues on the current code behavior or pull requests that will fix them
Projects
None yet
Development

No branches or pull requests

3 participants