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

Add Dynamic HTTP Status Code for JSON Responses in React\Http\Message\Response #507

Open
wants to merge 3 commits into
base: 1.x
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/Message/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,11 @@ public static function html($html)
* ```
*
* @param mixed $data
* @param int $status
* @return self
* @throws \InvalidArgumentException when encoding fails
*/
public static function json($data)
public static function json($data, $status = null)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should also work:

Suggested change
public static function json($data, $status = null)
public static function json($data, $status = self::STATUS_OK)

Copy link
Author

@emrancu emrancu Nov 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!! You are right.

{
$json = @\json_encode(
$data,
Expand All @@ -159,7 +160,7 @@ public static function json($data)
);
}

return new self(self::STATUS_OK, array('Content-Type' => 'application/json'), $json . "\n");
return new self($status ? $status : self::STATUS_OK, array('Content-Type' => 'application/json'), $json . "\n");
}

/**
Expand Down