Post

Go http.Redirect(...) http.StatusMovedPermanently 301

Go http.Redirect(...) http.StatusMovedPermanently 301

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package main

import (
	"fmt"
	"net/http"
)

// Redirects:
//  - StatusMultipleChoices   = 300 // RFC 7231, 6.4.1
//  - StatusMovedPermanently  = 301 // RFC 7231, 6.4.2
//  - StatusFound             = 302 // RFC 7231, 6.4.3
//  - StatusSeeOther          = 303 // RFC 7231, 6.4.4
//  - StatusNotModified       = 304 // RFC 7232, 4.1
//  - StatusUseProxy          = 305 // RFC 7231, 6.4.5
//  - StatusTemporaryRedirect = 307 // RFC 7231, 6.4.7
//  - StatusPermanentRedirect = 308 // RFC 7538, 3

func main() {
	http.HandleFunc("/", foo)
	http.HandleFunc("/bar", bar)
	http.Handle("/favicon.ico", http.NotFoundHandler())
	http.ListenAndServe(":8080", nil)

}

// Options for <form> and POST method in particular
// 	- <form method="POST" enctype="multipart/form-data">
// 	- <form method="POST" enctype="multipart/x-www-form-urlencoded">
// 	- <form method="POST" enctype="text/plain">

func foo(w http.ResponseWriter, r *http.Request) {
	fmt.Printf("Foo, request method: %v\n", r.Method)

}

func bar(w http.ResponseWriter, r *http.Request) {
	fmt.Printf("Bar, request method: %v\n", r.Method)
	http.Redirect(w, r, "/", http.StatusMovedPermanently) // status code: 301
}

This post is licensed under CC BY 4.0 by the author.