title: How make a http post using form data in flutter?
body: <p>I'm trying to do a http post request and I need to specify the body as <strong>form-data</strong>, because the server don't take the request as raw.</p>
  
  <p>This is what I'm doing:</p>
  
  <pre class="lang-dart prettyprint-override"><code>import 'dart:convert';
  
  import 'package:flutter/material.dart';
  import 'package:http/http.dart' as http;
  
  void main() =&gt; runApp(MyApp());
  
  class MyApp extends StatefulWidget {
    @override
    _MyAppState createState() =&gt; _MyAppState();
  }
  
  class _MyAppState extends State&lt;MyApp&gt; {
  
    postTest() async {
      final uri = 'https://na57.salesforce.com/services/oauth2/token';
      var requestBody = {
        'grant_type':'password',
        'client_id':'3MVG9dZJodJWITSviqdj3EnW.LrZ81MbuGBqgIxxxdD6u7Mru2NOEs8bHFoFyNw_nVKPhlF2EzDbNYI0rphQL',
        'client_secret':'42E131F37E4E05313646E1ED1D3788D76192EBECA7486D15BDDB8408B9726B42',
        'username':'example@mail.com.us',
        'password':'ABC1234563Af88jesKxPLVirJRW8wXvj3D'
      };
  
      http.Response response = await http.post(
          uri,
          body: json.encode(requestBody),
      );
  
      print(response.body);
    }
  
    @override
    Widget build(BuildContext context) {
      return MaterialApp(
        home: Container(
          child: Center(
            child: RaisedButton(
              child: Text('Press Here'),
              onPressed: (){
                postTest();
              },
            ),
          ),
        ),
      );
    }
  }
  </code></pre>
  
  <p>This is the actual response:</p>
  
  <pre><code>{
      "error": "unsupported_grant_type",
      "error_description": "grant type not supported"
  }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/FAWjo.png" rel="noreferrer"><img src="https://i.stack.imgur.com/FAWjo.png" alt="Actual response"></a></p>
  
  <p>This is the expected response:</p>
  
  <pre><code>{
      "access_token": "00D0b000000Bb08!AR8AQO.s8mAGXCbwV77FXNLQqc2vtl8g6_16miVbgWlQMsuNHsaf2IGLUwnMVXBOfAj19iznhqhwlPOi4tagvf7FFgiJJgoi",
      "instance_url": "https://na57.salesforce.com",
      "id": "https://login.salesforce.com/id/00D0b000000Bb08EAC/0050b000005nstiAAA",
      "token_type": "Bearer",
      "issued_at": "1567993324968",
      "signature": "1+Zd/dSh9i7Moh2U0nFJLdXkVHqPlPVU6emwdYzXDPk="
  }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/YxB2t.png" rel="noreferrer"><img src="https://i.stack.imgur.com/YxB2t.png" alt="Expected response"></a></p>
  
  <p>You can test this on postman switching the body between <strong>raw</strong> (you get the actual response) and <strong>form-data</strong> (you get the expected response)</p>
  
  <p>PS: The headers are temporary headers created by the client tool.</p>