doujiao3346 2019-08-24 01:04
浏览 63
已采纳

将字符串和[] byte组合为有效载荷

I'm trying to send an http request with a payload that contains 2 strings and a []byte. Any good ways of going about this? I've tried encryption/decryption (didn't work), converting the []byte to string (since the []byte is an image, it didn't work).

Visual representation:

string1 []byte string2

  • 写回答

1条回答 默认 最新

  • doufeinai6081 2019-08-24 01:36
    关注

    Here's an example using multipart request. I modified this from a piece of code I have that deals with JSON docs, so there may be some mistakes in it, but it should give you the idea:

            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)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于大棚监测的pcb板设计
  • ¥20 sim800c模块 at指令及平台
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计