How is use "422 Unprocessable Entity (WebDAV)" or any other custom http status code in golang

Issue

The go http package only supports the status code from RFC 2616. A lot of REST apis like github use 422 for bad input data. I would like to also do this, but I don’t see a good way of doing this in go. The options I see are

  1. Edit the source code to the http package and add it. This would be easy to do but would be bad to edit a core library.

  2. The http.Response struct has Status as a string StatusCode as an int. I think I could just set them in the responsce, but the http.Handler only has a RespnseWriter interface. It might be possible to make a http.Transport that has a RoundTripper that correctly sets the Response. Even if it is possible this seem like it would be a hacky to some degree.

So what is the best way of adding a custom http status code to go, or is it just a bad idea?

Solution

A number of status codes including 422 were added in go 1.7

https://github.com/golang/go/commit/b9ec0024fbc18dd94eff7240afd82fac6b4d8fdc

Answered By – masebase

Answer Checked By – Senaida (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.