federicotdn / verb

Organize and send HTTP requests from Emacs
https://melpa.org/#/verb
GNU General Public License v3.0
540 stars 20 forks source link

broken multipart data support. #42

Closed c4710n closed 2 years ago

c4710n commented 2 years ago

Hola, Thanks for providing this cool package.

Recently, I met a possible issue about multipart data.


As describe in doc, I create a request like this:

** Upload two files to user storage
post /{{(verb-var user-id)}}/upload
Content-Type: multipart/form-data; boundary={{(verb-boundary)}}

{{(verb-part "file" "file1.txt")}}
Content-Type: text/plain

{{(verb-read-file "documents/file1.txt")}}
{{(verb-part "file" "file2.xml")}}
Content-Type: application/xml

{{(verb-read-file "documents/file2.xml")}}
{{(verb-part)}}

But, the remote server complained that the request is not standard.

Then, I found:

  1. In HTTP protocol, the newline char is CRLF(\r\n).
  2. verb sends above request body which is using LF(\n) as the newline char.

Some servers can handle the non-standard format, some can't.


A possible resolution - insert \r manually:

This can be done in Emacs by using C-q C-m.

** Upload two files to user storage
post /{{(verb-var user-id)}}/upload
Content-Type: multipart/form-data; boundary={{(verb-boundary)}}

{{(verb-part "file" "file1.txt")}}^M
Content-Type: text/plain^M
^M
{{(verb-read-file "documents/file1.txt")}}^M
{{(verb-part "file" "file2.xml")}}^M
Content-Type: application/xml^M
^M
{{(verb-read-file "documents/file2.xml")}}^M
{{(verb-part)}}^M

It seems to work fine. But, verb-part blocks me.

In the implementation of verb-part, \n is used: https://github.com/federicotdn/verb/blob/f9ea5780ec65e6f30451514b72ce99619dd8457f/verb.el#L2227

I think it should be \r\n for more standard request.

I have rewrite this function on my local machine.


I think my solution is clumsy. Maybe we can have a better solution, for example:

federicotdn commented 2 years ago

Fixed in main - thanks for reporting this 👍🏻 .