为什么我的 go handler 没有生成有效的 FormData?

Why is my go handler not producing valid FormData?

我正在尝试让一个用 go 编写的服务器响应带有“mime/multipart”表单的“mime/multipart”表单。

使用下面的代码,我得到 Uncaught (in promise) TypeError: Could not parse content as FormData.

我的路线是这样的:

package main

import (
    "log"
    "mime/multipart"
    "net/http"
)

func handler(resp http.ResponseWriter, req *http.Request) {
    req.ParseMultipartForm(2097152)
    mw := multipart.NewWriter(resp)
    mw.WriteField("name", req.FormValue("user_name"))
    mw.WriteField("email", req.FormValue("user_email"))
    mw.WriteField("message", req.FormValue("user_message"))
    resp.Header().Set("Content-Type", mw.FormDataContentType())
    mw.Close()
}

func main() {
    http.HandleFunc("/", handler)
    log.Fatal(http.ListenAndServe(":8080", nil))
}

我的浏览器代码如下所示:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title>Your first HTML form</title>
        <link rel="stylesheet" href="form.css">
    </head>

    <body>

<form name="theForm" method="post">
 <ul>
  <li>
    <label for="name">Name:</label>
    <input type="text" id="name" name="user_name">
  </li>
  <li>
    <label for="mail">E-mail:</label>
    <input type="email" id="mail" name="user_email">
  </li>
  <li>
    <label for="msg">Message:</label>
    <textarea id="msg" name="user_message"></textarea>
  </li>
  <li class="button">
    <button type="submit">Send your message</button>
  </li>
 </ul>
</form>
      
      <pre id="response"></pre>
      
<script>
const form = document.forms["theForm"];

form.addEventListener( 'submit', function ( event ) {
  event.preventDefault();
  const fd = new FormData(form);
  fetch('/form-handler', {
    method: 'POST',
    body: fd
  })
  .then(response => response.formData())
  .then(data => document.getElementById("response").innerText = JSON.stringify(data));
});
  
  
</script>

    </body>
</html>

如果我将 JavaScript 更改为 .then(response => response.text),我会得到看起来正确的响应,所以不确定末尾是否缺少 CRLF 或什么?

您必须将 resp.Header().Set("Content-Type", mw.FormDataContentType()) 移动到第一个写入的上方。像这样:

func handler(resp http.ResponseWriter, req *http.Request) {
    req.ParseMultipartForm(2097152)
    mw := multipart.NewWriter(resp)
    resp.Header().Set("Content-Type", mw.FormDataContentType())
    mw.WriteField("name", req.FormValue("user_name"))
    mw.WriteField("email", req.FormValue("user_email"))
    mw.WriteField("message", req.FormValue("user_message"))
    mw.Close()
}

HTTP headers 在第一次写入 ResponseWriter 后无法修改。如果此时未设置 Content-Type,则默认为 Content-Type: text/plain; charset=utf-8,浏览器无法解析,因为它需要 Content-Type header[=17 中给出的边界=]