HTTP status codes

203 views
Skip to first unread message

Dejan Mijic

unread,
Jun 12, 2017, 7:22:24 PM6/12/17
to Go kit
Hello everyone,

What is the preferred way of setting the HTTP response status codes? As far as I understand, go-kit uses the "200 OK" as default response, but I'm not aware of any example illustrating responses mark with some other HTTP code, for example "201 created".

Looking forward for your response(s).

Kind regards,
Dejan Mijic

Peter Bourgon

unread,
Jun 14, 2017, 8:24:47 AM6/14/17
to Dejan Mijic, Go kit
Hi Dejan,

IMO, the "most correct" way, respecting the clean architecture rule of
inward-pointing dependencies, is to have a bit of code at the
transport layer which introspects received errors and decides on an
appropriate status code. Here[0] you can see such a bit of code, used
in the error encoder[1]. Note the general response encoder[2] also
needs to check responses to see if they should be encoded as errors;
that's accomplished by type-asserting on a custom errorer
interface[3].

[0] https://github.com/go-kit/kit/blob/a9ca672/examples/profilesvc/transport.go#L391
[1] https://github.com/go-kit/kit/blob/a9ca672/examples/profilesvc/transport.go#L380
[2] https://github.com/go-kit/kit/blob/a9ca672/examples/profilesvc/transport.go#L357-L362
[3] https://github.com/go-kit/kit/blob/a9ca672/examples/profilesvc/transport.go#L344-L350

You could also choose to have your errors and response types implement
a StatusCoder interface, and use the values directly, in order to skip
the mapping function in the transport layer. I think this is not so
great, because it breaks the dependency rule, by embedding
HTTP-specific logic in your supposedly pure business domain. But some
people find it a useful compromise.

Regards,
Peter.
> --
> You received this message because you are subscribed to the Google Groups
> "Go kit" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to go-kit+un...@googlegroups.com.
> To post to this group, send email to go-...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/go-kit/d510bcc4-d271-42d3-bf06-446eb7a351d4%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Dejan Mijic

unread,
Jun 14, 2017, 10:01:47 AM6/14/17
to Peter Bourgon, Go kit
Hello Peter,

Thank you, this did the job. While I didn't have any problems with
handling errors, I have managed to completely oversee the general
response encoder. Refactoring it as shown in the examples solved my problem.

Regards,

Dejan
Reply all
Reply to author
Forward
0 new messages