Gin: context.JSON adds a new line to end of JSON response

Created on 15 Jan 2020  路  3Comments  路  Source: gin-gonic/gin

Description

context.JSON() adds a new line character \n to end of JSON response

How to reproduce

Clone this repo and run

$ GIN_MODE=release go run gin-newline.go

Expectations

Newline character ("\n") must not get added to the end of {"message":"hello world"}

Actual result

new line character gets added

Environment

  • go version: 1.13.5
  • gin version (or commit ref): v1.5.0
  • operating system: mac os
bug

Most helpful comment

I am also facing an issue because of this redundant newline character addition.

I have a java service that sits in front a go service that is using gin framework.

  • The java service directly forwards the response headers from go service
  • The java service deserializes and then serializes the response json from go service using jackson which trims off the newline character.

Hence, the consumers of my java service are getting a content length mismatch issue between content-length header and actual content body.

gin-issue

All 3 comments

Before 1.5, context.JSON was using json.Marshal Refer Here. In 1.5 this was changed to use encoder.Encode(). Refer Change

Encoder adds a line at the end. Refer to this code

An issue about this is raised in the golang repository also.

Linking the PR which caused this issue in the name of performance improvement trading correctness and that too without performance numbers.

I am also facing an issue because of this redundant newline character addition.

I have a java service that sits in front a go service that is using gin framework.

  • The java service directly forwards the response headers from go service
  • The java service deserializes and then serializes the response json from go service using jackson which trims off the newline character.

Hence, the consumers of my java service are getting a content length mismatch issue between content-length header and actual content body.

gin-issue

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mastrolinux picture mastrolinux  路  3Comments

frederikhors picture frederikhors  路  3Comments

kekemuyu picture kekemuyu  路  3Comments

ccaza picture ccaza  路  3Comments

Bloomca picture Bloomca  路  3Comments