Returning empty json from a server - http 204

On a client in my Phoenix application in an ajax callback I have this:

    jResp = JSON.parse(xhr.responseText);

From a server I’m returning this:

# after a model has been inserted ....

put_status(conn, :no_content) |> json(%{})

On a client it throws an exception “Unexpected end of JSON input”

I’ve tried other things also:

put_status(conn, :no_content) |> json(nil)
put_status(conn, :no_content) |> json("")

and still it got passed to a client as an empty string which wasn’t valid json.

What should I return from a server as empty json?

204 does not allow a http body, so everything you pass has to be ignored by the client. It has to stop consuming bytes as soon as the end of headers are reached.

If you want to send 204 deal with empty body on client side or use a 200 with an empty json object or array.

1 Like