.NET Core web API – FromHeader, FromQuery & FromRoute

Validation of parameters

In the post net Core web API + Dapper -Web Caching I wondered why the GET returned a detailed 400 error and the HEAD didn’t when the eTag was invalid. I added a new controller to test this for .ASP.NET Core FromHeader, FromQuery and FromRoute attributes and the responses were consistent. The GET methods returned a detailed 400 error and the HEAD methods returned a simple 400 error when a value was invalid.

/*
	http://localhost:36739/api/FromHeaderFromQueryFromRoute/FromHeaderValidation
	User-Agent: Fiddler
	Host: localhost:36739
	eTag: USB456


	HTTP/1.1 400 Bad Request
	Content-Type: application/problem+json; charset=utf-8
	Server: Microsoft-IIS/10.0
	X-Powered-By: ASP.NET
	Date: Sun, 27 Jun 2021 05:39:06 GMT
	Content-Length: 257

	{
		"type":"https://tools.ietf.org/html/rfc7231#section-6.5.1",
		"title":"One or more validation errors occurred.",
		"status":400,
		"traceId":"00-82b38ed4019b004282e49c70489a76e0-d3674e4952022949-00",
		"errors":
		{
			"eTag":["eTagMust be not more than 5 characters long"]
		}
	}		
*/
[HttpGet("FromHeaderValidation")]
public ActionResult GetHeaderValidation([FromHeader(Name = "eTag")][MinLength(3, ErrorMessage = "The eTag header text must be at least 3 characters long")][MaxLength(5, ErrorMessage = "eTagMust be not more than 5 characters long")][Required(ErrorMessage ="The eTag field is required")] string request)
{
	return this.Ok(request);
}

/*
	http://localhost:36739/api/FromHeaderFromQueryFromRoute/FromHeaderValidation
	User-Agent: Fiddler
	Host: localhost:36739
	eTag: USB456


	HTTP/1.1 400 Bad Request
	Content-Length: 257
	Content-Type: application/problem+json; charset=utf-8
	Server: Microsoft-IIS/10.0
	X-Powered-By: ASP.NET
	Date: Sun, 27 Jun 2021 05:41:26 GMT
*/
[HttpHead("FromHeaderValidation")]
public ActionResult HeadHeaderValidation([FromHeader(Name = "eTag")][MinLength(3, ErrorMessage = "The eTag header text must be at least 3 characters long")][MaxLength(5, ErrorMessage = "eTagMust be not more than 5 characters long")][Required(ErrorMessage = "The eTag field is required")] string request)
{
	return this.Ok(request);
}

I was having a virtual coffee with a co-worker and they commented that a HEAD method doesn’t return a body so I went and checked the relevant Internet Engineering Task Force(IETF) Request For Comments(RFC) documentation.

https://www.ietf.org/rfc/rfc2616.txt

RFC 2616                        HTTP/1.1                       June 1999


9.4 HEAD

   The HEAD method is identical to GET except that the server MUST NOT
   return a message-body in the response. The metainformation contained
   in the HTTP headers in response to a HEAD request SHOULD be identical
   to the information sent in response to a GET request. This method can
   be used for obtaining metainformation about the entity implied by the
   request without transferring the entity-body itself. This method is
   often used for testing hypertext links for validity, accessibility,
   and recent modification.

   The response to a HEAD request MAY be cacheable in the sense that the
   information contained in the response MAY be used to update a
   previously cached entity from that resource. If the new field values
   indicate that the cached entity differs from the current entity (as
   would be indicated by a change in Content-Length, Content-MD5, ETag
   or Last-Modified), then the cache MUST treat the cache entry as
   stale.

The short answer is I’m stupid, the longer answer is above.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.