aurelia / http-client

A simple, restful, message-based wrapper around XMLHttpRequest.
MIT License
62 stars 59 forks source link

multipart/form-data queries have a null Content-Length in some cases #183

Open Plaristote opened 5 years ago

Plaristote commented 5 years ago

I'm submitting a bug report

Please tell us about your environment:

Current behavior: When using a FormData object as the body parameter for a query, the Content-Length header sometimes doesn't get set at all. The body is sent as expected: I checked for that. But as there is no Content-Length header, most servers will act as if the query was sent with an empty body.

In the following example, as long as the user doesn't input any file in the pictures field, the query will be sent with a Content-Length. However, if some files are uploaded, there's a chance that the Content-Length won't be set. It all depends on the specific files that are uploaded: I haven't been able to identify why, but some files aren't problematic, while others are.

Most of the files in my picture folder are affected by this issue. I've uploaded one for test purposes (and I did check that the issue still happened with the uploaded version): https://imagebin.ca/v/4WKYl7PyYMLM

Here's the code I've been using to reproduce the issue: formTest.html

<template>
  <form submit.trigger="send()">
    <label for="name">Name</label>
    <input type="text" name="name" ref="name" />
    <label for="pictures">Pictures</label>
    <input type="file" files.bind="pictures" multiple>
    <button>Send</button>
  </form>
</template>

formTest.js

import {HttpClient} from "aurelia-fetch-client";

export class FormTest {
  constructor() {
    this.http = new HttpClient;
  }

  send() {
    const data = new FormData;

    data.append("item[name]", this.name.value);
    if (this.pictures) {
      for (var i = 0 ; i < this.pictures.length ; ++i)
        data.append("item[pictures][]", this.pictures[i]);
    }
    this.http.fetch("/items", {
      method: "PUT",
      body: data
    });
    return false;
  }
}

Expected/desired behavior: The outgoing query with multipart/form-data content should always have a Content-Length set.