Closed xfuturomax closed 5 years ago
Is that true for all request types?
For DELETE requests certainly
Can we just replace 'null' string to empty one for all types? What is the reason we didn't do it at very beginning?
The 204 response must respond with an empty body, this is a priority over the json format specification.
Only 204 or we can extend this list?
protected function formatJson($response)
{
if ($response->data !== null) {
$options = $this->encodeOptions;
if ($this->prettyPrint) {
$options |= JSON_PRETTY_PRINT;
}
$response->content = Json::encode($response->data, $options);
} elseif ($response->content === null) {
if (in_array($response->getStatusCode(), [204])) {
$response->content = '';
} else {
$response->content = 'null';
}
}
}
Not sending content on 204 response doesn't depend on formatter and should be done regardless in Response::sendContent()
like:
if ($this->getStatusCode() === 204) {
return;
}
The case should be covered with a test.
The null response broke yiisoft/yii2-jui's AutoComplete object. A extra null was appended in the JSON response. Downgrading to 2.0.15.1 confirmed the issue is with 2.0.16. Will apply the patch and try again.
Yes, the problem remains for me. The AutoComplete widget expects a json object to display the popup menu. The object is returned, but there's also a null
string at the end of it (if I pass the response through console.log).
And the status code in that case is 200.
Why didn't you start new issue?
As I can understand, the problem is about response content (not about AutoComplete widget, it is important). There is "null string at the end" appears somehow. So, let us see your generating code with input params.
I didn't start a new issue, because I thought it's related to this issue.
Now, if response format is JSON (RESTfull API) and there is no content, i.e null, yii responds string 'null', and this is not right.Some software like Postman doesn't understand this.
/yiisoft/yii2/web/JsonResponseFormatter.php:121
elseif ($response->content === null) { $response->content = 'null'; }