title: Different server behavior from CURL and browser
body: <p>My server behaves differently when I do a login request with CURL and from the browser.</p>
  
  <p>When doing the following curl request:</p>
  
  <pre><code> curl -i -X POST 'http://localhost:8080/login' -d 'username=fred&amp;password=blah' -H 'content-type: application/x-www-form-urlencoded'
  </code></pre>
  
  <p>I get the expected result in form of:
  <img src="https://i.stack.imgur.com/fUvCv.png" alt="response from curl request"></p>
  
  <p>However when doing the same (or so I think) request from a browser form I see the following result in the browser debugger</p>
  
  <p><img src="https://i.stack.imgur.com/mwqBf.png" alt="canceled request">
  <img src="https://i.stack.imgur.com/dSs2y.png" alt="canceled request details"></p>
  
  <p>The code of the form used to make the request looks as follows</p>
  
  <pre><code>        &lt;form action="localhost:8080/login" method="POST"&gt;
            &lt;input type="text" name="username" placeholder="Username"&gt;
            &lt;input type="text" name="password" placeholder="Password"&gt;
            &lt;input type="submit" id="sign_in" value="Sign In"&gt;
          &lt;/form&gt;
  </code></pre>
  
  <p>I don't think this is a problem with the Origin of the request since I set <code>Access-Controll-Allow-Origin': *</code>.</p>
  
  <p>The entire code of the server can be found <a href="https://github.com/LukaszTrojanowski/linkShepherdServer" rel="nofollow noreferrer">here</a>. The code of the client <a href="https://github.com/LukaszTrojanowski/linkShepherdClient" rel="nofollow noreferrer">here</a>.</p>
  
  <p>I'am out of ideas. What could cause this problem?</p>
  
  <p>Edit: As suggested by  @Günter Zöchbauer in the comments below it is often necessary to do this sort of form submission via an HttpRequest. I wanted to add a answer with a ready solution to do so, and walked straight into another problem.
  The relevant html:</p>
  
  <pre><code>  &lt;form id="login_form" action="/login" method="POST" enctype="application/x-www-form-urlencoded"&gt;
      &lt;input type="text" name="username" placeholder="Username"&gt;
      &lt;input type="text" name="password" placeholder="Password"&gt;
      &lt;input type="submit" id="sign_in" value="Sign In" on-click="{{submitLogin}}"&gt;
    &lt;/form&gt;
  </code></pre>
  
  <p>The relevant dart code:</p>
  
  <pre><code>  /* Login box */
    login_form = querySelector('#login_form');
    login_form.onSubmit.listen(submitLogin);
  </code></pre>
  
  <p>and</p>
  
  <pre><code>/* Login code */
  void submitLogin(Event e){
    e.preventDefault();
  
    FormElement form = e.target as FormElement;
    print('inside of submitLogin');
  
    Uri loginUri = new Uri(scheme: 'http',
        host: 'localhost',
        port: 8080,
        path: 'login');
  
    print(loginUri);
    var request = new HttpRequest();
    request.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
    request.request(
        loginUri.toString(),
        method: form.method,
        sendData: new FormData(form)
    ).then(onDataLoaded);
  }
  
  void onDataLoaded(HttpRequest req){
    String response = req.responseText;
    print(response);
    //TODO: do visual confiramtion of login
  }
  </code></pre>
  
  <p>When comapring the CURL's from the original request (which worked) and the one done by the code above I noticed that the <code>Content-Type</code> is different.
  I tried to enforce the content type every way I knew but it keeps comming up like.</p>
  
  <pre><code>curl "http://localhost:8080/login" -H "Origin: http://localhost:8080" -H "Accept-Encoding: gzip, deflate" -H "Accept-Language: pl-PL,pl;q=0.8,en-US;q=0.6,en;q=0.4" -H "User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.0 (Dart) Safari/537.36" -H "Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryZ1seZo8pq0PZt3Fg" -H "Accept: */*" -H "Referer: http://localhost:8080/" -H "Connection: keep-alive" --data-binary "------WebKitFormBoundaryZ1seZo8pq0PZt3Fg"^
  "Content-Disposition: form-data; name=""username"""^
  
  "fred"^
  "------WebKitFormBoundaryZ1seZo8pq0PZt3Fg"^
  "Content-Disposition: form-data; name=""password"""^
  
  "blah"^
  "------WebKitFormBoundaryZ1seZo8pq0PZt3Fg--"^
  "" --compressed
  </code></pre>
  
  <p>Instead of.</p>
  
  <pre><code>curl "http://localhost:8080/login" -H "Origin: http://localhost:8080" -H "Accept-Encoding: gzip, deflate" -H "Accept-Language: pl-PL,pl;q=0.8,en-US;q=0.6,en;q=0.4" -H "User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.0 (Dart) Safari/537.36" -H "Content-Type: application/x-www-form-urlencoded" -H "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8" -H "Cache-Control: max-age=0" -H "Referer: http://localhost:8080/" -H "Connection: keep-alive" --data "username=fred&amp;password=blah" --compressed
  </code></pre>