-6

I have an HTTP server in Go in which when client is trying to login I have to authenticate credentials and in return i want to send success or failure. Later if any request come, I need to authenticate it using tokenID generated and on success i have to send a file.

I have tried the above using cookies. But cookies values are visible when opening cookies tab. So i need to send encrypt cookie. Please tell me a way to do so IF its possible.

Akhil Pathania
  • 542
  • 2
  • 5
  • 19
  • 4
    You cannot send more than one response. Sending multiple independent things is often done with [multipart responses](https://golang.org/pkg/mime/multipart/) (the same thing that happens with the request when you submit an HTML form with file inputs, for instance). – Peter Jun 14 '19 at 06:57
  • 1
    You might want to consider using templates if you want to serve html (`/html/home.html`) together with dynamic values (`name`, `password`). – mkopriva Jun 14 '19 at 07:24
  • 1
    Related: https://stackoverflow.com/q/53231492/13860 – Jonathan Hall Jun 14 '19 at 07:57

3 Answers3

1

sending username and password is a response , serving a file is a response too. You can't send two separate response at once.You can send an object as response containing username password and url of the file in the server.

Mostafa Solati
  • 1,235
  • 2
  • 13
  • 33
0

You just send only one response, but we can combine multipart responses in one with some pattern.

Like this:

{
  "username": "xxxx",
  "password": "xxxx",
  "file": "file uri"
}

Wei Huang
  • 174
  • 1
  • 6
0

ERROR:= "http: superfluous response.WriteHeader call". This error is coming as you cannot send two response for one request.

The best way to achieve what you are trying to do is by using cookies. Send the data in the form of cookies and Bingo. Your work will be done without errors/warnings.

expiration := time.Now().Add(time.Second * time.Duration(1000))
cookie := http.Cookie{Name: "Token", Value: "username", Expires: expiration}
http.SetCookie(w, &cookie)
usercookie := http.Cookie{Name: "usercookie", Value: "username", Expires: expiration}
http.SetCookie(w, &usercookie)
http.ServeFile(w, r, r.URL.Path[1:])

This Code will create a cookie and later you can access it. This is the right way of achieving what you want.

Akhil Pathania
  • 542
  • 2
  • 5
  • 19