Send Bad Request by REST

Asked

Viewed 228 times

0

A method of mine takes the field value. If this field is null or zero, a Bad Request must be sent to the user indicating the field ID and stating that it cannot be null. This is a REST service. I am assembling the code (a foreach) and inside this foreach it will spit out these BAD REQUEST if there is one. How do I generate a bad request within a Rest?

  • What I understand as "bad request" is code in an HTTP header of the response. And in this case the response would not even contain much more information. But this you could not spit several times into a foreach, only one, and then send the reply to the customer.

  • I usually return 200 and a JSON or XML with the additional information.

  • @Robertodecampos I’m sorry to say but to return saying that it’s "okay" is complicated. If you are responsible for the server and the client, then you can manage that very well. Otherwise, many applications/programmers assume that code 200 is successful. Use 4XX range error for request errors (client didn’t know what he wanted/client caused the error) and 5XX for server-side errors.

  • @pnet, a BAD REQUEST itself is just an HTTP code, if I’m not mistaken it’s 400. On some REST systems simply reporting this is enough. For example, since I only need to talk to client applications that are machines (without interacting with people), I just return the error code. But it is not your case, for you it is interesting to return also the message/ the error messages. I would risk a response in Java, where I can return an arbitrary string and an arbitrary HTTP code. But I don’t know C# at this point

  • The question is how to do this. There is a service that validates whether or not the guy filled a field. This field is required

  • Does anyone know of a link that explains how to send a bad request through a Rest? What should I do, or rather, how

  • How do I return a code 400, for example? How do I do this?

  • Are you using web.api? If you have your action you can return a Badrequest() as long as your return is Iactionresult.

  • @Gabrielcoletta, that’s right. Just ask me a question. I need to inform in this Badrequest products that are without serial. I mount an object with these values and put it in the parameter of the badRequest type: Return Badrequest(meu_object); that’s right?

  • Badrequest does not accept an object, just string. If you use Stringbuilder and mount the message it would work.

  • @bfavaretto O rfc7231 disagree. the server SHOULD send a representation containing an explanation of the error situation. But yes the answer is only one (there are not several bad requests). It could also be one Multi-Status

Show 6 more comments

1 answer

1

The MVC already makes the automatic validation of the Model by Voce. To get the errors of your Voce model you can access the property ModelState. Example:

var errors = string.join(" ", ModelState.SelectMany(s => s.Value.Errors)
    .Select(e => e.ErrorMessage))
if(errors.Length > 0){
    return BadRequest(errors);
}

To display friendly messages for the Voce user you must set the property ErrorMessage in your validation attributes, in your model. Example:

public class Modelo {
    [StringLength(1, ErrorMessage = "Introduza um nome válido")]
    public string Name{get;set;}
}

If this is not enough for you. You can always validate the model programmatically to know what the errors are. Example:

var context = new ValidationContext(model, null, null);
var results = new List<ValidationResult>();

bool valid = Validator.TryValidateObject(model, context, results, true);
if (valid)
{
    return Task.CompletedTask;
}

var errors = string.Join(" ", results.Select(e => e.ErrorMessage));
throw new InvalidOperationException($"Can not insert because data is not valid. {errors}");

Browser other questions tagged

You are not signed in. Login or sign up in order to post.