这个HTTP请求是否有效?

huangapple go评论104阅读模式
英文:

Is this HTTP request valid?

问题

我用swagger-codegen创建了一个Python服务器。我有一个接收mutlipart/form-data文件的端点。

我还使用go-swagger创建了一个用于测试的客户端。

创建一个要上传的文件:$ echo "123file content321" > data

然后使用客户端将文件上传到服务器。生成的HTTP请求如下所示:

POST /api/order/1/attachment HTTP/1.1
Host: 127.0.0.1:8080
User-Agent: Go-http-client/1.1
Transfer-Encoding: chunked
Accept: application/json
Content-Type: multipart/form-data; boundary=5f3f0ad86e6345b77c869cbe0a5e608f038354cf9ceab74ec2533d7555c0
Accept-Encoding: gzip

ff
--5f3f0ad86e6345b77c869cbe0a5e608f038354cf9ceab74ec2533d7555c0
Content-Disposition: form-data; name="file"; filename="data"
Content-Type: application/octet-stream

123file content321

--5f3f0ad86e6345b77c869cbe0a5e608f038354cf9ceab74ec2533d7555c0--

但是服务器不接受它并返回:

HTTP/1.0 400 BAD REQUEST
Connection: close
Content-Length: 120
Content-Type: application/problem+json
Date: Fri, 19 May 2017 15:15:44 GMT
Server: Werkzeug/0.12.1 Python/3.6.1
{
  "type": "about:blank",
  "title": "Bad Request",
  "detail": "Missing formdata parameter 'file'",
  "status": 400
}

所以请求没有被正确解析。但是当我使用swagger-ui时,文件被正确上传。是客户端的请求有问题,还是服务器有问题?

编辑:我认为缺少Content-Length或者BODY开头的ff可能不存在。

编辑2:swagger-ui的请求如下所示:

POST /api/order/1/attachment HTTP/1.1
Host: localhost:8080
Connection: keep-alive
Content-Length: 211
Origin: http://localhost:8080
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36
Content-Type: multipart/form-data; boundary=----WebKitFormBoundarypzmNwrDR7zzpZ7SJ
Accept: application/json
X-Requested-With: XMLHttpRequest
DNT: 1
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.8

------WebKitFormBoundarypzmNwrDR7zzpZ7SJ
Content-Disposition: form-data; name="file"; filename="data"
Content-Type: application/octet-stream

123file content321

------WebKitFormBoundarypzmNwrDR7zzpZ7SJ--
英文:

I've made an python server with swagger-codegen. I have one endpoint that receives an file with mutlipart/form-data

And also created an client with go-swagger for testing.

created an file to upload: $ echo "123file content321" > data

and used the client to upload the file to the server. The resulting HTTP request looks like this:

POST /api/order/1/attachment HTTP/1.1
Host: 127.0.0.1:8080
User-Agent: Go-http-client/1.1
Transfer-Encoding: chunked
Accept: application/json
Content-Type: multipart/form-data; boundary=5f3f0ad86e6345b77c869cbe0a5e608f038354cf9ceab74ec2533d7555c0
Accept-Encoding: gzip

ff
--5f3f0ad86e6345b77c869cbe0a5e608f038354cf9ceab74ec2533d7555c0
Content-Disposition: form-data; name="file"; filename="data"
Content-Type: application/octet-stream

123file content321

--5f3f0ad86e6345b77c869cbe0a5e608f038354cf9ceab74ec2533d7555c0--

but the server doesn't accept it and responds:

HTTP/1.0 400 BAD REQUEST
Connection: close
Content-Length: 120
Content-Type: application/problem+json
Date: Fri, 19 May 2017 15:15:44 GMT
Server: Werkzeug/0.12.1 Python/3.6.1
{
  "type": "about:blank",
  "title": "Bad Request",
  "detail": "Missing formdata parameter 'file'",
  "status": 400
}

So the request isn't parsed properly. But when I use the swagger-ui, the file is uploaded correctly. Is there problem with the client's request, or the server has a problem?

EDIT: I think that there is missing Content-Lenght or the ff at the beginning of the BODY might not be there

EDIT2: the swagger-ui request:

POST /api/order/1/attachment HTTP/1.1
Host: localhost:8080
Connection: keep-alive
Content-Length: 211
Origin: http://localhost:8080
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36
Content-Type: multipart/form-data; boundary=----WebKitFormBoundarypzmNwrDR7zzpZ7SJ
Accept: application/json
X-Requested-With: XMLHttpRequest
DNT: 1
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.8

------WebKitFormBoundarypzmNwrDR7zzpZ7SJ
Content-Disposition: form-data; name="file"; filename="data"
Content-Type: application/octet-stream

123file content321

------WebKitFormBoundarypzmNwrDR7zzpZ7SJ--

答案1

得分: 2

你发送的第一个请求是使用分块传输编码的HTTP/1.1请求。这意味着请求体由多个块组成,每个块以十六进制表示的大小为前缀,后跟\r\n,然后是数据,再次跟着\r\n。我不确定你展示的请求体开头的ff是否真的指定了后续数据的大小(即255字节)。但是,最后一个大小为0的块缺失,所以这个请求是不完整的。但也许你只是在这个问题中省略了缺失的部分。

除此之外,服务器的响应是HTTP/1.0版本。分块传输编码仅在HTTP/1.1中定义,这意味着HTTP/1.0服务器将无法理解这个请求。即使是HTTP/1.1服务器,也不一定能理解请求中的分块传输编码,尽管它们应该能够理解。

你展示的第二个请求(由Chrome创建)没有使用分块传输编码,而是在HTTP头中使用Content-length指定了头部的长度。这是你应该采用的方式,因为这适用于所有的网络服务器,包括HTTP/1.0服务器。

英文:

The first request you send is a HTTP/1.1 request using chunked transfer encoding. This means the body is consisting of multiple chunks where each chunk is prefixed by the size in hex followed by \r\n followed by the data and again \r\n. I'm not sure if the ff at the beginning of the body you show really specifies the size of the following data (i.e. 255 bytes). But, the last chunk with a size of 0 is missing, so this request is incomplete. But maybe you just omitted the missing part from this question only.

Apart from that the server is responding with version HTTP/1.0. Chunked transfer encoding is only defined for HTTP/1.1 which means that this request will not be understood by a HTTP/1.0 server. And not even all HTTP/1.1 server will understand chunked transfer encoding in the request even if they should.

The second request you show (created by Chrome) does not use chunked transfer encoding but specifies instead the length of the header using Content-length in the HTTP header. That's the way you should go since this works with all web servers, including HTTP/1.0 servers.

答案2

得分: 0

根据你发布的两个请求,我会首先尝试在你的Go请求中设置Content-Length并进行测试。我之前遇到过ArangoDB HTTP API不接受没有正确内容长度值的请求的问题。

如果成功了,那太好了。

否则,我会继续检查请求中的ff,但我会先专注于Content-Length头部。

英文:

Based on the two requests you have posted I would attempt to set the Content-Length on your go request first and test that. I've run into issues before with the ArangoDB HTTP API not accepting requests without a correct content length value.

If the succeeds then yay.

Otherwise, that ff in your request is the next thing I'd look at getting rid of. But I'd focus on the Content-Length header first.

huangapple
  • 本文由 发表于 2017年5月19日 23:26:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/44073801.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定