When is it appropriate to respond with a HTTP 412 error?

0/5 No votes

Report this app

Description

It’s unclear to me when it’s best to and shouldn’t return a HTTP 412: Precondition Failed, error for an online service? I’m considering of utilizing it when validating information. For instance, if a consumer POST’s XML information and that information is lacking a required information factor, then responding with a 412 and an outline of the error.

Does that align with the spirit of responding with an HTTP 412, or ought to one thing else be used (e.g. one other http error code or net software exception)?

Deal with all 4YY errors as consumer errors. Deal with 412 Precondition Failed as 400 Unhealthy Request. That’s, the consumer (GUI / script) has despatched the unsuitable enter to an API.
HTTP error 412 is used when you’re utilizing row variations or timestamps to keep away from concurrency points. In the event you despatched the net service a file and embrace the row model within the header, the net service can examine that row model to the present row model. If the values do not match, you reject the request to replace with a 412 error.

In the event you take a look at RFC 2616 you may see a variety of request headers that can be utilized to use circumstances to a request:

If-Match
If-Modified-Since
If-None-Match
If-Vary
If-Unmodified-Since

These headers include ‘preconditions’, permitting the consumer to inform the server to solely full the request if sure circumstances are met. For instance, you employ a PUT request to replace the state of a useful resource, however you solely need the PUT to be actioned if the useful resource has not been modified by another person since your most up-to-date GET.

The response standing code 412 (Precondition Failed) is usually used when these preconditions fail.

Your instance feels like an invalid request (i.e. the consumer has submitted information that’s invalid due to lacking values). A standing code of 400 (Unhealthy Request) is extra acceptable right here IMO.

 

 

 

Advertisement

Leave a Reply

Your email address will not be published. Required fields are marked *

Advertisement

Copyright © 2004-2021 Tvapk.xyz, All Rights Reserved.