Skip to content

Instantly share code, notes, and snippets.

@nidhi-canopas
Created July 19, 2022 10:17
Show Gist options
  • Select an option

  • Save nidhi-canopas/7bebba72d073b1ac72f6722dacbeee4d to your computer and use it in GitHub Desktop.

Select an option

Save nidhi-canopas/7bebba72d073b1ac72f6722dacbeee4d to your computer and use it in GitHub Desktop.
func MockJsonPost(c *gin.Context, content interface{}) {
c.Request.Method = "POST"
c.Request.Header.Set("Content-Type", "application/json")
c.Set("user_id", 1)
jsonbytes, err := json.Marshal(content)
if err != nil {
panic(err)
}
// the request body must be an io.ReadCloser
// the bytes buffer though doesn't implement io.Closer,
// so you wrap it in a no-op closer
c.Request.Body = io.NopCloser(bytes.NewBuffer(jsonbytes))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment