1

While making a POST request to Golang API, if I send stringy-fied JSON data it returns success but when I send JSON data it returns error with status 403.

Please help me understanding this behavior and how can I send JSON data using a POST request method.

File: main.go

package main

import (
    "devmgmtv2/auth"
    "github.com/gorilla/handlers"
    "github.com/gorilla/mux"
    "log"
    "net/http"
)

func main() {
    router := mux.NewRouter()
    auth.AuthInit(router)
    ssid.SsidInit(router)
    headersOk := handlers.AllowedHeaders([]string{"X-Requested-With"})
    originsOk := handlers.AllowedOrigins([]string{"*"})
    methodsOk := handlers.AllowedMethods([]string{"GET", "HEAD", "POST", "PUT", "OPTIONS"})
    router.HandleFunc("/auth/login", Login).Methods("POST", "OPTIONS")
    log.Fatal(http.ListenAndServe(":8000", handlers.CORS(headersOk, originsOk, methodsOk)(router)))
}

func Login(w http.ResponseWriter, r *http.Request) {
    //Create User Struct
    var user User
    json.NewDecoder(r.Body).Decode(&user)
    userPassword := getUserPassword(user.User)
    // call get value for that user
    // check for equality if true, return the structure
    // if false return error
    if user.Password == userPassword {
        w.Header().Set("Content-Type", "text/plain")
        w.Write([]byte("success"))
    } else {
        http.Error(w, "Forbidden", http.StatusForbidden)
    }
}
6
  • 2
    What does "It works when I pass my JSON in String format i.e. raw text but when it Send a JSON" mean? JSON is a text format and is send always as "string" or "text". What did you do exactly? Commented Dec 20, 2017 at 9:40
  • When I was sending data after Doing JSON.stringify(data) it worked, but when I was sending data as is . It wasn't working. But it used to work with NodeJS server Commented Dec 20, 2017 at 9:44
  • It sounds like your client-side code isn't producing JSON, then, and there's nothing wrong with your Go. Commented Dec 20, 2017 at 9:51
  • @Flimzy this is my body data -> let data = { "user": user, "password": password } Commented Dec 20, 2017 at 10:01
  • 2
    If you want to consume JSON you must send JSON. I have no idea how Node serializes an arbitrary object. So: Simply send JSON (and not random garbage). Commented Dec 20, 2017 at 10:06

1 Answer 1

4

When sending JSON to any http server you always have to use JSON.stringify().

Not doing so will result in sending [object Object]... There are client libraries that do this kind of heavy lifting for you, but behind the scenes the JSON is always send as a string.


Node.JS handles it the same way... it receives the string representation and usually something like body parser is run on the incoming request to extract the JSON from the string. So it might happen here behind the scenes, but it still happens.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.