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

AbortWithStatusJSON and JSON returns wrong response headers #1053

Closed
mymtw opened this issue Aug 3, 2017 · 3 comments
Closed

AbortWithStatusJSON and JSON returns wrong response headers #1053

mymtw opened this issue Aug 3, 2017 · 3 comments

Comments

@mymtw
Copy link

mymtw commented Aug 3, 2017

gin latest version. 1.2

  1. Need to add docs how to use ctx method AbortWithStatusJSON
	var ucr messages.UsersCreateRequest
	if e := c.BindJSON(&ucr); e != nil {
		c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{
			"errors": "someError",
		})
	}
        fmt.Println(1)

response should be application/json; charset=utf-8 BUT it gives content-type == 'text/plain; charset=utf-8'

  1. 1 I thought I will not need to write return If I will use AbortWithStatusJSON.
  1. I'm getting in response headers content-type == 'text/plain; charset=utf-8' even if I using c.JSON:
	var ucr messages.UsersCreateRequest
	if e := c.BindJSON(&ucr); e != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"errors": "bsalslasalksaslas"
		})
		return
	}
@mymtw mymtw changed the title AbortWithStatusJSON not returning response. Need to add docs for ctx method AbortWithStatusJSON AbortWithStatusJSON and JSON returns wrong response headers Aug 3, 2017
@appleboy
Copy link
Member

appleboy commented Aug 4, 2017

This PR #1047 can resolve this issue.

@appleboy
Copy link
Member

appleboy commented Aug 4, 2017

#1041 (comment) can resolve your problem.

@mymtw
Copy link
Author

mymtw commented Aug 4, 2017

@appleboy thank you, works.
I'm closing this issue and:
I didn't find in README.md (docs) anything about using this ShouldBindWith.
it will be better to update the doc, even the note like if you wanna get content-type == 'application/json' in response, then you need to use c.ShouldBindWith(&something, binding.JSON); instead of c.BindJSON.
Cuz, on my frontend I'm checking content-type in response, in my tests on backend not. Frontend didn't work correctly for me.
Thx

@mymtw mymtw closed this as completed Aug 4, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants