Search code examples
multipartform-datasuperagent

Superagent: PUT-ing multipart form data


Is it possible to do a PUT request with multipart form data?

With Superagent I would expect the following to work, but it doesn't.

var request = Request
  .put("http://localhost:8080/upload_file")
  .field("name", file.name)
  .field("size", file.size)
  .attach("file", file.file, file.file.name)
  .accept("application/json")

If I do a post, it works. The difference is the Content-Type. With the successful post request the Content-Type is multipart/form-data; boundary=------WebKitFormBoundaryXg34NkBFcYWq60mH.

If I were to set this manually how would I know what the boundary should be? It seems to be automatically generated by Superagent.


Solution

  • You should probably do a POST, per Tum's comment.

    If I were to set this manually how would I know what the boundary should be? It seems to be automatically generated by Superagent.

    You should let Superagent manage that by itself - don't try to set the type yourself, leave off the type call and it will include the correct boundary identifier when it sets it as multi-part itself.