title: How to post x-www-form-urlencoded in Flutter
body: <p>I am trying to send a <code>POST</code> request to an API to create an account.<br>
  The request is working well, it should look like this :  </p>
  
  <p>Bulk Edit Mode :<br>
  <a href="https://i.stack.imgur.com/1Q19G.png" rel="noreferrer"><img src="https://i.stack.imgur.com/1Q19G.png" alt="Bulk Edit mode"></a></p>
  
  <p>Key-Value Edit mode :<br>
  <a href="https://i.stack.imgur.com/DkIMg.png" rel="noreferrer"><img src="https://i.stack.imgur.com/DkIMg.png" alt="Key-Value Edit mode"></a></p>
  
  <p>There are also 9 headers that are auto-generated, so I did not show them, but I can take another screen if you need to.</p>
  
  <p>My request looks like this :</p>
  
  <pre><code>import 'dart:convert' as convert ;
  
  import 'package:my_project/requests/utils.dart';
  import 'package:http/http.dart' as http;
  
  
  Future&lt;String&gt;      createUser(String firstName, String name, String mail,
      String password, String confirmPassword, String birthDate,
      String phone) async {
    String              url = BASE_URL + "createUser" ; // Don't worry about BASE_URL, the final url is correct
  
    Map&lt;String, dynamic&gt;    formMap = {
      "name": name,
      "surname": firstName,
      "mail": mail,
      "password": password,
      "birth": birthDate,
      "phone": phone,
      "confirmPassword": confirmPassword
    } ;
  
    http.Response    response = await http.post(
      url,
      body: convert.jsonEncode(formMap),
      headers: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      encoding: convert.Encoding.getByName("utf-8"),
    );
    print("RESPONSE ${response.statusCode} ; BODY = ${response.body}");
  
    return (response.body) ;
  
  }
  </code></pre>
  
  <p>Here is my print result :  </p>
  
  <pre><code>I/flutter ( 6942): RESPONSE 307 ; BODY =  
  </code></pre>
  
  <p>As you can see, I am getting a 307 error, and the problem does not come from the server, as it worked with Postman.</p>
  
  <p>Am I sending this <code>form-urlencoded POST</code> request correctly ?</p>
  
  <p>I also tried :  </p>
  
  <pre><code>http.Response    response = await http.post(
      url,
      body: "name=$name&amp;surname=$firstName&amp;mail=$mail&amp;password=$password&amp;birth=$birthDate&amp;phone=$phone&amp;confirmPassword=$confirmPassword",
      headers: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      encoding: convert.Encoding.getByName("utf-8"),
    );
  </code></pre>
  
  <p>but with the same results. I tried too :</p>
  
  <pre><code>http.Response    response = await http.post(
      url,
      body: formMap,
      headers: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      encoding: convert.Encoding.getByName("utf-8"),
    );
  </code></pre>
  
  <p>with same result again.<br>
  What am I doing wrong ?</p>
  
  <p>EDIT :</p>
  
  <p>I tried FoggyDay answer, here is my request now :</p>
  
  <pre><code>final client = HttpClient() ;
  final request = await client.postUrl(Uri.parse(url));
  request.headers.set(HttpHeaders.contentTypeHeader, "application/x-www_form-urlencoded");
  request.followRedirects = true ;
  request.write(formMap);
  final response = await request.close();
  print("STATUS CODE = ${response.statusCode}");
  </code></pre>
  
  <p>However I still have a 307 error. Did I create the right request ?</p>
  
  <p>EDIT 2 :</p>
  
  <p>As asked, I printed location as follow :  </p>
  
  <pre><code>final client = HttpClient() ;
  final request = await client.postUrl(Uri.parse(url));
  request.headers.set(HttpHeaders.contentTypeHeader, "application/x-www_form-urlencoded");
  request.followRedirects = true ;
  request.write(formMap);
  final response = await request.close();
  print("STATUS CODE = ${response.statusCode}");
  print("Response headers = ${response.headers}");
  </code></pre>
  
  <p>And I get :  </p>
  
  <pre><code>I/flutter ( 7671): STATUS CODE = 307
  I/flutter ( 7671): Response headers = location: /app/createUser/
  I/flutter ( 7671): date: Tue, 26 May 2020 09:00:29 GMT
  I/flutter ( 7671): content-length: 0
  I/flutter ( 7671): server: Apache/2.4.41 (Amazon) OpenSSL/1.0.2k-fips
  </code></pre>
  
  <p>The thing is I am already making a call on /app/createUser... ('/app/' is in BASE_URL)</p>