title: HTTP POST request to PHP REST API using Flutter
body: <p>So I have been at this for way way too long but I just can't figure out why this isn't working what I'm trying to do is send some JSON post data to an existing PHP REST API that I've created and then read that JSON data like <code>$_POST["username"]</code> and I can't really change how the API works as it's used by another application which it works perfectly with so it's something to do with my flutter code but I'm not sure what as I started learning it today.</p>
  
  <p>My flutter code:</p>
  
  <pre><code>import 'package:http/http.dart' as http;
  
  http.Response response = await http.post(
      'https://path.to.php.file',
      headers: &lt;String, String&gt;{
        'Content-Type': 'application/json',
      },
      body: jsonEncode(&lt;String, String&gt;{
        "formname": "login",
        "username": globals.currentUsername, // value = futurelucas4502
        "password": globals.currentPassword, // value = password
        "datetime": DateFormat("yyyy-MM-dd HH:mm:ss").format(DateTime.now()) // MySql Datetime format
      }),
    );
  
    debugPrint("Response: " + response.body);
    debugPrint("Status: " + (response.statusCode).toString());
  </code></pre>
  
  <p>Cut down PHP code:</p>
  
  <pre><code>&lt;?php
      ini_set('display_errors', 1);
      ini_set('display_startup_errors', 1);
      error_reporting(E_ALL);
      echo file_get_contents('php://input');
      echo '&lt;br&gt;';
      echo $_POST["username"];
  ?&gt;
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>Response: {"formname":"login","username":"futurelucas4502","password":"password","datetime":"2020-05-12 00:01:33"}&lt;br&gt;&lt;br /&gt;
  
  &lt;b&gt;Notice&lt;/b&gt;:  Undefined index: username in &lt;b&gt;/storage/ssd2/545/12156545/public_html/temp/index.php&lt;/b&gt; on line &lt;b&gt;7&lt;/b&gt;&lt;br /&gt;
  
  Status: 200
  </code></pre>
  
  <p>What can I change in my flutter code to make this work?
  <br />
  <strong>EDIT:</strong> Just for clarity I do want to handle an application/json request the only reason <code>file_get_contents</code> is there is to test if the PHP API was actually receiving the data.</p>