Golang redirect problem


import (
	"fmt"
	"net/http"
	"io"
)

func main() {
	http.HandleFunc("/", func(res http.ResponseWriter, req *http.Request) {
		htmp := `<!doctype html>

		<html lang="en">
		<head>
		  <meta charset="utf-8">
		  <title>MAIN</title>
		</head>

		<body>
		  <form action="/" enctype="multipart/form-data" method="POST">
    		  	Photo: <input type="file"  id="filedata" name="filedata">
    	 	  	<input type="submit"	value="Upload Photo">
		  </form>
		</body>
		</html>`
		io.WriteString(res, htmp)
		if req.Method == "POST"{
			fmt.Println(req.ContentLength)
			if req.ContentLength > 50 * 1024{
				x := req.ContentLength - (50*1024)
				fmt.Printf("Bigger than allowed by %v bytes", x)
				http.Redirect(res, req, "/bad", http.StatusFound)
				return
			}else{
				fmt.Println("Your file is OK")
			}
		}
	})
	
	http.HandleFunc("/bad", func(res http.ResponseWriter, req *http.Request) {
		htmp := `<!doctype html>

		<html lang="en">
		<head>
		  <meta charset="utf-8">
		  <title>BAD</title>
		</head>
		<body>
		  <h1>BAD</h1>
		</body>
		</html>`
		io.WriteString(res, htmp)
	})

	http.ListenAndServe(":8080", nil)
}

It gives me this error ““http: multiple response.WriteHeader calls”” and doesn’t redirect to “/bad”.
I don’t know what’s wrong.
In this file I upload an image and whether It is bigger than 50K I want to redirect to the route “/bad”.

By the time you call http.Redirect, you’ve already started to write the body to the client with io.WriteString(res, htmp).

To fix this, switch on req.Method before sending anything to the client.

switch req.Method {
case POST:
      // handle redirect
case GET:
     io.WriteString(res, htmp)
default:
     http.Error(res, 405, "method not allowed: "+req.Method)
}
1 Like

Many thanks Dave. Works perfect your solution :slight_smile:


import (
	"fmt"
	"net/http"
	"io"
)

func main() {
	http.HandleFunc("/", func(res http.ResponseWriter, req *http.Request) {
		htmp := `<!doctype html>

		<html lang="en">
		<head>
		  <meta charset="utf-8">
		  <title>MAIN</title>
		</head>

		<body>
		  <form action="/" enctype="multipart/form-data" method="POST">
    		  	Photo: <input type="file"  id="filedata" name="filedata">
    	 	  	<input type="submit"	value="Upload Photo">
		  </form>
		</body>
		</html>`
		switch req.Method {
		case "POST":
			fmt.Println(req.ContentLength)
			if req.ContentLength > 50 * 1024{
				x := req.ContentLength - (50*1024)
				fmt.Printf("Bigger than allowed by %v bytes", x)
				http.Redirect(res, req, "/bad", http.StatusSeeOther)
				return
			}
			http.Redirect(res, req, "/ok", http.StatusSeeOther)
		case "GET":
			io.WriteString(res, htmp)
		default:
			http.Error(res, "method not allowed: " + req.Method, 405)
		}

	})
	http.HandleFunc("/bad", func(res http.ResponseWriter, req *http.Request) {
		htmp := `<!doctype html>

		<html lang="en">
		<head>
		  <meta charset="utf-8">
		  <title>BAD</title>
		</head>
		<body>
		  <h1>BAD</h1>
		</body>
		</html>`
		io.WriteString(res, htmp)
	})
	http.HandleFunc("/ok", func(res http.ResponseWriter, req *http.Request) {
		htmp := `<!doctype html>

		<html lang="en">
		<head>
		  <meta charset="utf-8">
		  <title>OK</title>
		</head>
		<body>
		  <h1>OK</h1>
		</body>
		</html>`
		io.WriteString(res, htmp)
	})

	http.ListenAndServe(":8080", nil)
}

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.