Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Http 组合有效负载的字符串和[]字节_Http_Go_Byte - Fatal编程技术网

Http 组合有效负载的字符串和[]字节

Http 组合有效负载的字符串和[]字节,http,go,byte,Http,Go,Byte,我正在尝试发送一个http请求,其有效负载包含2个字符串和一个[]字节。有什么好办法吗?我尝试过加密/解密不起作用,将[]字节转换为字符串,因为[]字节是图像,所以它不起作用 视觉表现: string1 []字节 string2下面是一个使用多部分请求的示例。我根据处理JSON文档的一段代码对其进行了修改,因此其中可能有一些错误,但它应该能让您了解: body := bytes.Buffer{} writer := multipart.NewWriter(&

我正在尝试发送一个http请求,其有效负载包含2个字符串和一个[]字节。有什么好办法吗?我尝试过加密/解密不起作用,将[]字节转换为字符串,因为[]字节是图像,所以它不起作用

视觉表现:

string1 []字节
string2

下面是一个使用多部分请求的示例。我根据处理JSON文档的一段代码对其进行了修改,因此其中可能有一些错误,但它应该能让您了解:

        body := bytes.Buffer{}
        writer := multipart.NewWriter(&body)
        hdr := textproto.MIMEHeader{}
        hdr.Set("Content-Type", "text/plain")
        part, _ := writer.CreatePart(hdr)
        part.Write(data1)

        hdr = textproto.MIMEHeader{}
        hdr.Set("Content-Type", <image type>)
        part, _ = writer.CreatePart(hdr)
        part.Write(imageData)

        ... // Add more parts if you need to
        writer.Close()

        request, _ := http.NewRequest(http.MethodPost, url, &body)
        request.Header.Set("Content-Type", fmt.Sprintf("multipart/mixed;boundary=%s", writer.Boundary()))

        hcli := http.Client{}
        rsp, err := hcli.Do(request)

使用多部分消息@bserdar您能否提供一个与http.NewRequest一起使用的示例?您能否编写一些使用http.NewRequest的快速代码,其正文的格式如下:``string[]byte string``我的示例有两部分,文本部分和图像部分。只需添加另一个文本部分。你知道,这的另一端,接收器,也必须将其作为多部分读取。