You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Looking a little deeper, the HttpResponse class (from the http lib), has the reason phrase parameter to be set. However the Response class (from shelf) does not have this property to be configured. And of course, in the _writeResponse of the shelf_io the mapping to said property is not done.
Fixing this is as easy as adding the property 'reasonPhrase' to the Response and later mapping it to HttpResponse:
/// The response returned by a [Handler].classResponseextendsMessage {
/// The HTTP status code of the response.finalint statusCode;
/// THIS PROPERTY IS THE ONE TO ADD /// The reason phrase for the response. /// /// If no reason phrase is explicitly set, a default reason phrase is provided.finalString? reasonPhrase;
///(AL THE OTHER STUFF IN THE CLASS)/// Constructs an HTTP response with the given [statusCode]. /// /// [statusCode] must be greater than or equal to 100. /// /// {@macro shelf_response_body_and_encoding_param}Response(
this.statusCode, {
this.reasonPhrase, //ADDED reason phrase paramObject? body,
Map<String, /* String | List<String> */Object>? headers,
Encoding? encoding,
Map<String, Object>? context,
}) :super(body, encoding: encoding, headers: headers, context: context) {
if (statusCode <100) {
throwArgumentError('Invalid status code: $statusCode.');
}
}
And also we need to added in write function:
Future<void> _writeResponse(
Response response, HttpResponse httpResponse, String? poweredByHeader) {
if (response.context.containsKey('shelf.io.buffer_output')) {
httpResponse.bufferOutput =
response.context['shelf.io.buffer_output'] asbool;
}
httpResponse.statusCode = response.statusCode;
httpResponse.reasonPhrase = response.reasonPhrase ??''; ///NEW LINE TO ADD IN ORDER TO SET UP PROPERTY// An adapter must not add or modify the `Transfer-Encoding` parameter, but// the Dart SDK sets it by default. Set this before we fill in// [response.headers] so that the user or Shelf can explicitly override it if// necessary.
httpResponse.headers.chunkedTransferEncoding =false;
(MORESTUFFOFMETHOD)
Whit this couple of changes when we run the same example it give us the following response:
This way we have a better explanation of the response codes.
This solution clearly don't affect others status codes:
Would this be a worthy feature to add to the package to create a PR?
Or was it designed this way specifically for some reason?
Thanks in advance
The text was updated successfully, but these errors were encountered:
When we return a response with a status like 422, the reason phrase is empty and no details are specified.
This is the example to reproduce it:
When making the request in postman it looks like this:
When we create the same service in other framework, like spring for example, the response is more explained like this.
Having this example:
This give us a more explained response like this:
Looking a little deeper, the
HttpResponse
class (from the http lib), has the reason phrase parameter to be set. However theResponse
class (from shelf) does not have this property to be configured. And of course, in the_writeResponse
of theshelf_io
the mapping to said property is not done.Fixing this is as easy as adding the property 'reasonPhrase' to the
Response
and later mapping it toHttpResponse
:And also we need to added in write function:
Whit this couple of changes when we run the same example it give us the following response:
This way we have a better explanation of the response codes.
This solution clearly don't affect others status codes:
Would this be a worthy feature to add to the package to create a PR?
Or was it designed this way specifically for some reason?
Thanks in advance
The text was updated successfully, but these errors were encountered: