Post Api Getting Invalid Character '-' in Numeric Literal

Post Api Getting Invalid Character '-' in Numeric Literal

I'm trying to make a POST function to insert data in my database using go.

My Model:

type Funcstruct {
    Name      string `json:"Name"`
    Status    string `json:"Status"`
}

My post function:

//POST FUNC
func (app *App) createFunc(w http.ResponseWriter, r *http.Request) {

    decoder := json.NewDecoder(r.Body)

    var data Func
    err := decoder.Decode(&data)
    if err != nil {
        panic(err)
    }

    Name := data.Name
    Status := data.Status

    database, err := db.CreateDatabase()
    if err != nil {
        log.Fatal("Not connect to db")
    }
    _, err = database.Exec("INSERT INTO `func` (Name, Status) VALUES ($1, $2)", Name, Status)
    if err != nil {
        log.Fatal("Error inserting data")
    }

    log.Println("Alright!")
    w.WriteHeader(http.StatusOK)
}

So i make one go build and after i make a ./nameproject

So, i go in the PostMan and make a post http request passing the Name and Status in form-data but i receive:

2019/07/12 22:20:38 http: panic serving [::1]:54600: invalid character '-' in numeric literal

3

4 Answers

I experience the same issue while using form data in postman,I finally resolved to using json and it worked

You should change form-data to raw->JSON and send like this:

{
    "Name" : "morefeizi",
    "Status" : "1"
}
1

I experience the same issue while using multipart/form-data in postman. And finally I solved it.

I use gin-gonic but I think it's the same issue.

my code before

type XXXReq struct {
    FiledA string `form:"file_a"`
}
var req XXXReq
if err := ctx.ShouldBindJSON(&req); err != nil {
    DOSOMETHING
}

my code after

type XXXReq struct {
    FiledA string `form:"file_a"`
}
var req XXXReq
if err := ctx.ShouldBind(&req); err != nil {
    DOSOMETHING
}

So, maybe you SHOULD NOT use decoder := json.NewDecoder(r.Body) to parse data.

Send JSON Data from PostMan to unmarshal

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Marcus Vance
Author

Marcus Vance

Marcus Vance is a cybersecurity auditor and technology writer dedicated to educating the public about online safety, data privacy regulations, enterprise security, and emerging cyber threats.