Gin: Responding with HTTP 204

Created on 27 Jan 2015  路  8Comments  路  Source: gin-gonic/gin

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

question

Most helpful comment

We write a 204 by using the gin.ResponseWriter in the context object.

c.Writer.WriteHeader(http.StatusNoContent)

All 8 comments

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)

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)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

gplume picture gplume  路  3Comments

kekemuyu picture kekemuyu  路  3Comments

mastrolinux picture mastrolinux  路  3Comments

oryband picture oryband  路  3Comments

lilee picture lilee  路  3Comments