Go中的多部分http请求

Go中的多部分http请求,go,Go,我试图使用Go编写一个实用程序,通过向服务器发出多部分http请求来验证和上传文件。一切似乎都很顺利,只是文件没有到达服务器。进一步看,请求中的多部分似乎是空的。代码和请求输出如下。我的Go代码中缺少了什么 代码:(我更改了URL…) 我打印的第一个东西是字节缓冲区b,它包含多部分数据,从这里看一切都很好。(这是一个xml文件) 然后我打印出请求结构,在这里我看到MultipartForm是空的 &{Method:GET URL:https://mysite/home/ Proto: P

我试图使用Go编写一个实用程序,通过向服务器发出多部分http请求来验证和上传文件。一切似乎都很顺利,只是文件没有到达服务器。进一步看,请求中的多部分似乎是空的。代码和请求输出如下。我的Go代码中缺少了什么

代码:(我更改了URL…)

我打印的第一个东西是字节缓冲区b,它包含多部分数据,从这里看一切都很好。(这是一个xml文件)

然后我打印出请求结构,在这里我看到MultipartForm是空的

&{Method:GET URL:https://mysite/home/ Proto: ProtoMajor:0 ProtoMinor:0 Header:map[Authentication:[Bearer: DY0LCJL0g] Content-Type:[multipart/form-data; boundary=83451b003d8e5cc38c0e8f60ad318e522cad4818cf293745c84ec36d26d5] Referer:[http://mysite/home/]] Body:<nil> GetBody:<nil> ContentLength:0 TransferEncoding:[] Close:false Host: Form:map[] PostForm:map[] MultipartForm:<nil> Trailer:map[] RemoteAddr: RequestURI: TLS:<nil> Cancel:<nil> Response:0xc42018a360 ctx:<nil>}
&{方法:获取URL:https://mysite/home/ Proto:ProtoMajor:0 ProtoMajor:0标头:映射[身份验证:[承载者:DY0LCJL0g]内容类型:[多部分/表单数据;边界=83451b003d8e5cc38c0e8f60ad318e522cad4818cf293745c84ec36d26d5]引用者:[http://mysite/home/]]正文:GetBody:ContentLength:0传输编码:[]关闭:假主机:窗体:映射[]PostForm:map[]MultipartForm:Trailer:map[]RemoteAddr:RequestURI:TLS:Cancel:Response:0xc42018a360 ctx:}

我非常怀疑服务器是否真的什么也没有收到。具有nil正文的打印正文的行为应在
http.Response

    // Request is the request that was sent to obtain this Response.
    // Request's Body is nil (having already been consumed).
    // This is only populated for Client requests.
    Request *Request
如果要调试发出的请求主体,应该使用模拟服务器或代理


另一方面,您的代码尝试登录将不起作用。它不维护登录信息的cookie,因此以后的请求无法使用这些cookie。

请您在不打印缓冲区的情况下重试?在我读过的某个地方,缓冲区只能使用一次!不要忽略错误我检查并打印错误。也许,服务器希望您正确设置内容类型,而不是
application/octet stream
,即
text/xml
application/xml
?在这种情况下,请使用
CreateFormFile
,而不是使用
CreatePart
,然后手动设置内容类型。另外,打印请求方法是
GET
,但在代码中,您使用的是
POST
。你打印的是正确的吗?
200 OK
&{Method:GET URL:https://mysite/home/ Proto: ProtoMajor:0 ProtoMinor:0 Header:map[Authentication:[Bearer: DY0LCJL0g] Content-Type:[multipart/form-data; boundary=83451b003d8e5cc38c0e8f60ad318e522cad4818cf293745c84ec36d26d5] Referer:[http://mysite/home/]] Body:<nil> GetBody:<nil> ContentLength:0 TransferEncoding:[] Close:false Host: Form:map[] PostForm:map[] MultipartForm:<nil> Trailer:map[] RemoteAddr: RequestURI: TLS:<nil> Cancel:<nil> Response:0xc42018a360 ctx:<nil>}
    // Request is the request that was sent to obtain this Response.
    // Request's Body is nil (having already been consumed).
    // This is only populated for Client requests.
    Request *Request