0

enter image description hereWhen I use http://localhost:8080/login?id=ddfd@vcv.com&pwd=dccccf in postman or use it in android app I am getting 404. On curl I get

{"name":"Miss Moneypenny","email":"ddfd@vcv.com","password":"dccccf","mobile":27,"address":"dscsdacc"}

I am not able to understand what can I do to achieve json output in postman and on other platforms like Apps in ios as well as android when I use this api and also on the browser window.

My Main.go code

func getSession() *mgo.Session {
    s, err := mgo.Dial("mongodb://localhost")

    if err != nil {
        panic(err)
    }
    return s
}
func main() {
    r := httprouter.New()
    uc := controllers.NewUserController(getSession())
    r.GET("/login", uc.LoginUser)
    http.ListenAndServe(":8080", r)

}

code in controller/user.go

type UserController struct {
    session *mgo.Session
}

func NewUserController(s *mgo.Session) *UserController {
    return &UserController{s}
}
func (uc UserController) LoginUser(w http.ResponseWriter, request *http.Request, params httprouter.Params) {
       dump,err :=httputil.DumpRequest(request, true)
    if err != nil {
    http.Error(w, fmt.Sprint(err), http.StatusInternalServerError)
    return
    }
    fmt.Println("Request Dump:\n", string(dump))
    encodedValue := request.URL.Query().Get("id")
    pwd := request.URL.Query().Get("pwd")
    emailId, err := url.QueryUnescape(encodedValue)
    if err != nil {
        log.Fatal(err)
        return
    }
    u := models.User{}

    if err := uc.session.DB("go-web-dev-db").C("users").FindId(emailId + pwd).One(&u); err != nil {
        w.WriteHeader(404)
        return
    }

    uj, err := json.Marshal(u)
    if err != nil {
        fmt.Println(err)
    }

    w.Header().Set("Content-Type", "application/json")
    w.WriteHeader(http.StatusOK) // 200
    fmt.Fprintf(w, "%s\n", uj)
}

code in model/user.go

type User struct {
    Name     string `json:"name" bson:"name"`
    Email    string `json:"email" bson:"_id"`
    Password string `json:"password" bson:"password"`
    Mobile   int    `json:"mobile" bson:"mobile"`
    Address  string `json:"address" bson:"address"`
}

After using dump when I am using i am using curl 'http://localhost:8080/login?id=ddfd@vcv.com&pwd=dccccf' I get :-

Request Dump:
GET /login?id=ddfd@vcv.com&pwd=dccccf HTTP/1.1
Host: localhost:8080
Accept: */*
User-Agent: curl/7.69.1

After using dump when I am using i am using http://localhost:8080/login?id=ddfd@vcv.com&pwd=dccccf in postman I get :-

Request Dump:
GET /login?id=ddfd@vcv.com&pwd=dccccf HTTP/1.1
Host: localhost:8080
Accept: */*
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Postman-Token: 8e925738-b8db-4656-9f53-813f4cd53a80
User-Agent: PostmanRuntime/7.24.1
Mohit Yadav
  • 158
  • 3
  • 11
  • 1
    I'd add more logging to compare the query parameters and request handling flow in both cases. – bereal May 07 '20 at 08:25
  • 1
    Try dumping the raw request so that you can see the same thing that the Go server sees. Use [`httputil.DumpRequest(request, false)`](https://golang.org/pkg/net/http/httputil/#DumpRequest) and print the output of that to the console, then execute both the curl and postman requests and then compare the two outputs. – mkopriva May 07 '20 at 08:26
  • 1
    As an aside, you should consider implementing a proper login strategy, even if this is a toy project it serves only to engender a bad habit. – mkopriva May 07 '20 at 08:36
  • 1
    There is no need to call QueryUnescape. Don't send passwords in the query string. Send a POST request and put them into the body. – Peter May 07 '20 at 08:37
  • @mkopriva I am getting `GET /login?id=ddfd@vcv.com HTTP/1.1 Host: localhost:8080 Accept: */* User-Agent: curl/7.69.1 ` in curl but still getting 404 in postman – Mohit Yadav May 07 '20 at 08:43
  • @Peter I had added the QueryUnescape in the code because android app was sending %40 instead of @. about the POST, I will try it. – Mohit Yadav May 07 '20 at 08:52
  • 1
    @MohitYadav the curl's request dump is not showing the pwd, so you're sending different request with curl compared to that with postman. And that coupled with the fact that you're using `FindId(emailId + pwd)` should clarify why one works but the other doesn't. – mkopriva May 07 '20 at 08:58
  • 1
    @MohitYadav note that I suggested `httputil.DumpRequest` as a tool for *debuggin* the problem, not as something that will fix the problem. So the comment *"but still getting 404 in postman"* is unnecessary and irrelevant, instead you should show the curl and postman dumps side by side, and best not in the comments section but in the quesiton itself, by updating it. – mkopriva May 07 '20 at 09:05
  • @mkopriva i am using `curl http://localhost:8080/login?id=ddfd@vcv.com&pwd=dccccf ` in both – Mohit Yadav May 07 '20 at 09:07
  • 1
    @MohitYadav it doesn't matter what you're using, what matters is what's in the dump. You may think you're sending all the query parameters with curl, but maybe you've incorrectly formatted the command. If it is not in the dump then it was not sent, simple as that! – mkopriva May 07 '20 at 09:09
  • @mkopriva I have updated the question but still if i am getting only one param in dump. How can I solve this to get both the params in the code? – Mohit Yadav May 07 '20 at 09:17
  • @MohitYadav in the shell `&` has a special meaning and is interpreted differently than you expect, which means that everything after `curl http://localhost:8080/login?id=ddfd@vcv.com` becomes a separate shell "command". You can fix this by wrapping the url in single quotes. i.e. `curl 'http://localhost:8080/login?id=ddfd@vcv.com&pwd=dccccf'` – mkopriva May 07 '20 at 09:23
  • Does this answer your question? [How to include an '&' character in a bash curl statement](https://stackoverflow.com/questions/13339469/how-to-include-an-character-in-a-bash-curl-statement) – mkopriva May 07 '20 at 09:26
  • @MohitYadav also note that you've placed the `DumpRequest` call in the wrong place, when debugging it should always be the first thing you do, it should be at the top of the function body. Otherwise you will not see the dump if your db returns an error and your handler exits as is the case with the postman requests. – mkopriva May 07 '20 at 09:40
  • 1
    @MohitYadav and to add to that, why send the output of the dump to the client? You're debuggin, print it to the console, using plain old `fmt.Println`. What you should have done, to properly debug the incoming requests, is something like this: https://play.golang.com/p/Ro-7tR0RF_V – mkopriva May 07 '20 at 09:51
  • @mkopriva thanks!! for the help in debugging the issue. I have updated the question accordingly.. still i am getting 404 in postman – Mohit Yadav May 07 '20 at 09:58
  • 1
    URL.Query() already unescapes the values. If you unescape again you get incorrect values. + will be changed to space, for instance. – Peter May 07 '20 at 10:07
  • @MohitYadav if you don't want `404` then don't use `emailId + pwd` in `.FindId`, use just `emailId`. If `emailId + pwd` is *by design* what you should be using then that means that there's no user in the database whose id is equal to `emailId + pwd`. – mkopriva May 07 '20 at 10:14
  • @mkopriva i got both the params using wrapping curl in single quotes. About `.FindId` i was trying to verify if the user entered correct password and emailid. That's why i am using `emailId + pwd`. – Mohit Yadav May 07 '20 at 10:20
  • @MohitYadav I don't know how `FindId` is implemented, nor do I know how you are storing user's ids, but your current implementation of a basic login seems *completely* wrong. You should look for, using a search engine, articles on how to implement basic login functionality with Go and MongoDB. – mkopriva May 07 '20 at 10:31
  • @mkopriva Kudos!! Thanks for all the help if working fine with `.findId(email)` perfwctly will do sum research in login functionality with Go and MongoDB and post the Proper Result as the Answer... Really a big thanks to you and peter for helping me. – Mohit Yadav May 07 '20 at 10:37
  • @mkopriva whenever u r free please take a look at this also https://stackoverflow.com/questions/61802648/getting-unexpected-directory-name-in-json-output-in-golang-server – Mohit Yadav May 16 '20 at 14:13

0 Answers0