Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

context.JSON adds a new line to end of JSON response #2209

Closed
kishaningithub opened this issue Jan 15, 2020 · 4 comments · Fixed by #2228
Closed

context.JSON adds a new line to end of JSON response #2209

kishaningithub opened this issue Jan 15, 2020 · 4 comments · Fixed by #2228
Labels

Comments

@kishaningithub
Copy link

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
@jpninanjohn
Copy link

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.

@KaushikNeelichetty
Copy link
Contributor

KaushikNeelichetty commented Feb 7, 2020

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

@sayeedhussain
Copy link

sayeedhussain commented Feb 9, 2020

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

KaushikNeelichetty pushed a commit to KaushikNeelichetty/gin that referenced this issue Feb 10, 2020
thinkerou added a commit that referenced this issue Feb 21, 2020
#2209 (#2228)

* ignore IntelliJ idea generated files

* update JSON renderer to use Marshall() instead of Encode(). Fix #2209

* Revert "ignore IntelliJ idea generated files"

This reverts commit e7bd017.

Co-authored-by: Bo-Yi Wu <appleboy.tw@gmail.com>
Co-authored-by: thinkerou <thinkerou@gmail.com>
byebyebruce pushed a commit to byebyebruce/gin that referenced this issue Mar 25, 2020
gin-gonic#2209 (gin-gonic#2228)

* ignore IntelliJ idea generated files

* update JSON renderer to use Marshall() instead of Encode(). Fix gin-gonic#2209

* Revert "ignore IntelliJ idea generated files"

This reverts commit e7bd017.

Co-authored-by: Bo-Yi Wu <appleboy.tw@gmail.com>
Co-authored-by: thinkerou <thinkerou@gmail.com>
@igorfraa
Copy link

for those who do not appreciate this change and expect JSONs ending with newline, a straightforward middleware that appends a newline after every response:

// MiddlewareNewlineAppender returns a Gin middleware that should be executed just after the handler.
// It appends a newline character to the response for 'curl/*' useragent.
func MiddlewareNewlineAppender() gin.HandlerFunc {
	return func(ctx *gin.Context) {
		ctx.Next() // First call the next handler in the chain.
		useragent := ctx.Request.UserAgent() // Then perform the actual job of appending a newline character.
		if strings.HasPrefix(useragent, "curl") {
                    _, _ = ctx.Writer.WriteString("\n")
		}
	}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants