I am having trouble understanding how to respond with HTTP 204 (no content) using gin.Context.
Tried context.JSON(204, nil) or just gin.H{} but both complain that the body is not empty.
Error #01: http: request method or response status code does not allow body
Meta: [<nil>]
Any help or pointer much appreciated!
Thanks
c.Abort(204) works but feels very wrong to be calling Abort on a request.
+1
We write a 204 by using the gin.ResponseWriter in the context object.
c.Writer.WriteHeader(http.StatusNoContent)
You can use Data
https://github.com/gin-gonic/gin/blob/master/context.go#L362
http://play.golang.org/p/Y0sDeUcLjv
c.Data(204, gin.MIMEHTML, nil)
All of these work but what if I want a JSON content type anyway?
I wish there were a easier way to do this:
// Sends a JSON response with no content
c.Writer.Header().Set("Content-Type", "application/json; charset=utf-8")
c.Writer.WriteHeader(http. StatusNoContent)
Maybe a test that if c.JSON(204, nil) is passed, it outputs nothing instead of null.
Or make a gin.NoContent constant?
@eexit working on it 馃槈
cc / @appleboy
@eexit done!
@matejkramny, you can use c.Status(http.StatusNoContent)
Most helpful comment
We write a 204 by using the gin.ResponseWriter in the context object.