Golang中无法写入Cookie的问题如何解决

Golang中无法写入Cookie的问题如何解决 我正在尝试向网页添加两个Cookie,如下所示,但都没有生效:

package main

import (
	"encoding/json"
	"fmt"
	"log"
	"math/rand"
	"net/http"
	"net/smtp"
	"strconv"
)

type verification struct {
	Email string
}

func verify(w http.ResponseWriter, r *http.Request) {
	decoder := json.NewDecoder(r.Body)
	var user verification
	err := decoder.Decode(&user)
	if err != nil {
		panic(err)
	}
	log.Println(user.Email)

	w.WriteHeader(http.StatusCreated)
	w.Header().Set("Content-Type", "application/json")
	resp := make(map[string]string)

	min := 1000
	max := 9999
	code := strconv.Itoa(rand.Intn(max-min) + min)
	// 确保允许安全性较低的应用:
	// https://myaccount.google.com/lesssecureapps
	from := "myemail@gmail.com"
	pass := "mypasscode"
	to := user.Email
	body := fmt.Sprintf("Hello, your verification code is: %v", code)

	msg := "From: " + from + "\n" +
		"To: " + to + "\n" +
		"Subject: Hello there\n\n" +
		body

	err = smtp.SendMail("smtp.gmail.com:587",
		smtp.PlainAuth("Cocoon Solutions", from, pass, "smtp.gmail.com"),
		from, []string{to}, []byte(msg))

	if err != nil {
		resp["Error"] = fmt.Sprintf("%s", err)
		jsonResp, err := json.Marshal(resp)
		if err != nil {
			log.Fatalf("Error happened in JSON marshal. Err: %s", err)
		}
		w.Write(jsonResp)
		return
	}

	log.Print("verification code sent, check your email")

	cookie := http.Cookie{Name: "email", Value: "user.Email", Path: "/"} // user.Email
	http.SetCookie(w, &cookie)

	cookie2 := http.Cookie{Name: "VerificationCode", Value: "code", Path: "/"} // code
	http.SetCookie(w, &cookie2)

	resp["VerificationCode"] = code
	jsonResp, err := json.Marshal(resp)
	if err != nil {
		log.Fatalf("Error happened in JSON marshal. Err: %s", err)
	}
	w.Write(jsonResp)
}

已知在另一段代码中,它工作得很好:

package main

import (
	"fmt"
	"html/template"
	"net/http"
)

type register struct {
	Company, Name, Mobile, Whatsapp, Country, City string
}

func registration(w http.ResponseWriter, r *http.Request) {
	fmt.Println("method:", r.Method)                                                             //获取请求方法
	tmpl := template.Must(template.ParseFiles(fmt.Sprintf("%v", "templates/registration.html"))) // index.html
	if r.Method == "GET" {
		//	t, _ := template.ParseFiles("login.gtpl")
		tmpl.Execute(w, nil)
	} else {
		r.ParseForm()
		fmt.Println(r.Form)
		// map[city:[Dammam] company:[Aujan] mobile:[059] name:[Hasan] whatsapp:[059]]
		// 登录的逻辑部分
		fmt.Println("company:", r.Form["company"]) // HTML表单中的字段名
		fmt.Println("name:", r.Form["name"])
		fmt.Println("mobile:", r.Form["mobile"])
		fmt.Println("whatsapp:", r.Form["whatsapp"])
		fmt.Println("country:", r.Form["country"])
		fmt.Println("city:", r.Form["city"]) // city: [Dammam]

		//map[string]string
		content := register{
			Company:  r.PostForm.Get("company"),
			Name:     r.PostForm.Get("name"),
			Mobile:   r.PostForm.Get("mobile"),
			Whatsapp: r.PostForm.Get("whatsapp"),
			Country:  r.PostForm.Get("country"),
			City:     r.PostForm.Get("city"),
		}

		cookie := http.Cookie{Name: "logged", Value: "true", Path: "/"}
		http.SetCookie(w, &cookie)

		http.Redirect(w, r, "/index", http.StatusSeeOther) // 重定向到另一个URL
	}
}

有什么想法吗?


更多关于Golang中无法写入Cookie的问题如何解决的实战教程也可以访问 https://www.itying.com/category-94-b0.html

1 回复

更多关于Golang中无法写入Cookie的问题如何解决的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html


问题在于你在写入Cookie之前已经调用了w.WriteHeader(http.StatusCreated)w.Write(jsonResp)。一旦调用了WriteHeaderWrite,HTTP头部就会被发送,后续的SetCookie调用将无法生效。

正确的顺序应该是先设置Cookie,再设置状态码和写入响应体。以下是修正后的代码:

func verify(w http.ResponseWriter, r *http.Request) {
    decoder := json.NewDecoder(r.Body)
    var user verification
    err := decoder.Decode(&user)
    if err != nil {
        panic(err)
    }
    log.Println(user.Email)

    w.Header().Set("Content-Type", "application/json")
    resp := make(map[string]string)

    min := 1000
    max := 9999
    code := strconv.Itoa(rand.Intn(max-min) + min)
    
    // 邮件发送逻辑...
    
    if err != nil {
        resp["Error"] = fmt.Sprintf("%s", err)
        jsonResp, err := json.Marshal(resp)
        if err != nil {
            log.Fatalf("Error happened in JSON marshal. Err: %s", err)
        }
        w.WriteHeader(http.StatusInternalServerError)
        w.Write(jsonResp)
        return
    }

    log.Print("verification code sent, check your email")

    // 先设置Cookie
    cookie := http.Cookie{Name: "email", Value: user.Email, Path: "/"}
    http.SetCookie(w, &cookie)

    cookie2 := http.Cookie{Name: "VerificationCode", Value: code, Path: "/"}
    http.SetCookie(w, &cookie2)

    // 然后设置状态码和写入响应
    resp["VerificationCode"] = code
    jsonResp, err := json.Marshal(resp)
    if err != nil {
        log.Fatalf("Error happened in JSON marshal. Err: %s", err)
    }
    w.WriteHeader(http.StatusCreated)
    w.Write(jsonResp)
}

另外,你的Cookie值设置也有问题。原代码中使用了字符串字面量而不是变量:

// 错误写法
cookie := http.Cookie{Name: "email", Value: "user.Email", Path: "/"}
cookie2 := http.Cookie{Name: "VerificationCode", Value: "code", Path: "/"}

// 正确写法
cookie := http.Cookie{Name: "email", Value: user.Email, Path: "/"}
cookie2 := http.Cookie{Name: "VerificationCode", Value: code, Path: "/"}

第二个示例代码能正常工作的原因是它使用了http.Redirect,这会自动处理头部写入的顺序。

回到顶部