doumou1864 2018-01-09 12:11
浏览 169

带有XML正文和文件作为附件的HTTP POST

I have an issue. I am working on a client in Go which contacts a SOAP server. I am supposed to send an HTTP POST request to the server with SOAP message in the body. And I have to also attach one file with the request. How do I do that?

Till now, I am able to just put SOAP message in the request, but not getting how to include the file as well in the request. Below is a code to generate the request. How do I include the file in this request?

payload := strings.NewReader(soapDataString)

req, _ := http.NewRequest("POST", endPointUrl, payload)

req.SetBasicAuth("user", "password")
req.Header.Add("content-type", "text/xml")
req.Header.Add("cache-control", "no-cache")
req.Header.Add("SOAPAction", "")

return req
  • 写回答

1条回答 默认 最新

  • doulu8415 2018-01-09 13:37
    关注

    Either you should use a SOAP library that supports adding attachment or you should know SOAP standard to include an attachment.

    From https://www.w3.org/TR/SOAP-attachments

    The following example shows a SOAP 1.1 message with an attached facsimile image of the signed claim form (claim061400a.tiff):

    MIME-Version: 1.0
    Content-Type: Multipart/Related; boundary=MIME_boundary; type=text/xml;
            start="<claim061400a.xml@claiming-it.com>"
    Content-Description: This is the optional message description.
    
    --MIME_boundary
    Content-Type: text/xml; charset=UTF-8
    Content-Transfer-Encoding: 8bit
    Content-ID: <claim061400a.xml@claiming-it.com>
    
    <?xml version='1.0' ?>
    <SOAP-ENV:Envelope
    xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
    <SOAP-ENV:Body>
    ..
    <theSignedForm href="cid:claim061400a.tiff@claiming-it.com"/>
    ..
    </SOAP-ENV:Body>
    </SOAP-ENV:Envelope>
    
    --MIME_boundary
    Content-Type: image/tiff
    Content-Transfer-Encoding: binary
    Content-ID: <claim061400a.tiff@claiming-it.com>
    
    ...binary TIFF image...
    --MIME_boundary--
    

    It's a multipart MIME type. You could use mime/multipart package to generate a multipart with ease.

    Here is another snippet that creates a multipart form that includes an arbitrary file from file system (from this blog).

    file, err := os.Open(path)
    if err != nil {
        return nil, err
    }
    defer file.Close()
    
    body := &bytes.Buffer{}
    writer := multipart.NewWriter(body)
    part, err := writer.CreateFormFile(paramName, filepath.Base(path))
    if err != nil {
        return nil, err
    }
    _, err = io.Copy(part, file)
    
    err = writer.Close()
    if err != nil {
        return nil, err
    }
    
    req, err := http.NewRequest("POST", uri, body)
    req.Header.Set("Content-Type", writer.FormDataContentType())
    return req, err
    
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值