Pauli
Pauli

Reputation: 1265

Go gin-framework: Testing query and POST with cURL

I'm trying a code example in the README of gin framework ("Another example: query + post form"):

package main

import (
    "fmt"
    "github.com/gin-gonic/gin"
)

func main() {
    router := gin.Default()

    router.POST("/post", func(c *gin.Context) {
        id := c.Query("id")
        page := c.DefaultQuery("page", "0")
        name := c.PostForm("name")
        message := c.PostForm("message")

        fmt.Printf("id: %s; page: %s; name: %s; message: %s\n", id, page, name, message)
    })
    router.Run(":8080")
}

Testing the code with cURL:

curl -d "name=Maru&message=Nice" 0.0.0.0:8080/post?id=2&page=3

The server returns: id: 2; page: 0; name: Maru; message: Nice.

Is the curl testing correct? Why isn't page in the returned value equal to 3?

Upvotes: 1

Views: 2023

Answers (1)

user142162
user142162

Reputation:

The ampersand (&) is a special character in your shell. It causes the previous command to run in the background. Your shell was interpreting the command as:

curl -d "name=Maru&message=Nice" 0.0.0.0:8080/post?id=2 & # run curl in the background
page=3 # set page=3

Escaping the character will give you the expected result:

curl -d "name=Maru&message=Nice" "0.0.0.0:8080/post?id=2&page=3"
curl -d "name=Maru&message=Nice" 0.0.0.0:8080/post?id=2\&page=3

Upvotes: 2

Related Questions