go http 服务器标头内容类型设置为 multipart/formdata 但在客户端获取 ContentType: text/plain

2024年 2月 13日 72.8k 0

go http 服务器标头内容类型设置为 multipart/form-data 但在客户端获取 content-type: text/plain

在进行网络开发中,经常需要使用 HTTP 请求来传输数据。在某些情况下,我们可能需要将请求的内容类型设置为 multipart/form-data,并在客户端获取 Content-Type 为 text/plain 的数据。这样的设置可以通过在 HTTP 服务器的标头中进行配置实现。在 PHP 中,我们可以通过相应的函数和方法来实现这个目标。在本文中,php小编西瓜将为您介绍如何在 PHP 中设置 HTTP 服务器的标头内容类型为 multipart/form-data,并在客户端获取 Content-Type 为 text/plain 的数据。

问题内容

Go 服务器将 header Content-type 设置为 multipart/form-data

router.HandleFunc("/certificates", serveFilesHandler).Methods("GET")

func serveFilesHandler(w http.ResponseWriter, r *http.Request) {

currentDir, err := os.Getwd()
if err != nil {
log.Fatal("Can not find the current directory: ", err)
}
pathToCertifs := "../certificates"

// Create a multipart writer for the response
multipartWriter := multipart.NewWriter(w)

files := []string{"client.key", "server.key", "rootCA.key"}

for _, filename := range files {
filePath := filepath.Join(currentDir, pathToCertifs, filename)

fmt.Println("see the filePath: ", filePath)

// Open the file
file, err := os.Open(filePath)
if err != nil {}
defer file.Close()

// Create a new form file part
part, err := multipartWriter.CreateFormFile("files", filename)
if err != nil {}

// Copy the file content to the part
_, err = io.Copy(part, file)
if err != nil {}
}

// Set the content type for the response
w.Header().Set("Content-Type", multipartWriter.FormDataContentType())
fmt.Println("Content-Type set to:", w.Header().Get("Content-Type"))
// printout Content-Type set to: multipart/form-data; boundary=7b326

// Close the multipart writer
multipartWriter.Close()
}

登录后复制

但是在客户端我得到了

Expected multipart response, but received: text/plain;

登录后复制

但是有效负载位于主体中

body, err := ioutil.ReadAll(resp.Body)
if err != nil {}
Content-Type: text/plain; charset=utf-8
Response Body:
--aee406774ba6a054d52e39a3cdb72f42d32bd30828adbfb1982d278cab56
Content-Disposition: form-data; name="files"; filename="client.key"
Content-Type: application/octet-stream

-----BEGIN PRIVATE KEY-----
MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQDNg4ZaTLC/GdLK
xzFDIyPlYyKs/hUXpPkZAQk+3gnvmBaDuMNq2jd2nQoQohmk1zIuD8oj9se5L+3P

登录后复制

但我无法按部分获取它,因为内容类型不是 multipart/form-data,所以这不起作用

multipartReader := multipart.NewReader(resp.Body, boundaryFromContentType(contentType))

// Read each part
for {
part, err := multipartReader.NextPart()
if err != nil {
break
}
defer part.Close()
......

登录后复制

我缺少什么,谢谢?

ps:这个问题需要更多细节,我觉得已经很清楚了,所以我添加了这一行,之后可能会起作用。

解决方法

在将任何内容写入响应正文之前,必须设置 HTTP 响应标头。一旦提交了标头(当您向响应正文写入内容时),您就无法设置或更改标头。

您创建多部分编写器以及所有部分和内容,然后设置响应标头,然后仅关闭多部分编写器。关闭只是为了完成多部分消息并写入尾部边界,但其很多内容可能已经被写入并提交。

在向多部分编写器添加/写入任何内容之前移动设置标头:

// Create a multipart writer for the response
multipartWriter := multipart.NewWriter(w)

w.Header().Set("Content-Type", multipartWriter.FormDataContentType())

// Now proceed to add files...

登录后复制

以上就是go http 服务器标头内容类型设置为 multipart/form-data 但在客户端获取 Content-Type: text/plain的详细内容,更多请关注每日运维网(www.mryunwei.com)其它相关文章!

相关文章

JavaScript2024新功能:Object.groupBy、正则表达式v标志
PHP trim 函数对多字节字符的使用和限制
新函数 json_validate() 、randomizer 类扩展…20 个PHP 8.3 新特性全面解析
使用HTMX为WordPress增效:如何在不使用复杂框架的情况下增强平台功能
为React 19做准备:WordPress 6.6用户指南
如何删除WordPress中的所有评论

发布评论