title: Flutter PUT HTTP request from file
body: <p>Need some help troubleshooting an HTTP request in Flutter using the PUT method sending an image file on the body to a NextCloud server via API.</p>
  
  <p>As always, i tested sending the request via POSTMAN to see what's happening. It worked ok selecting "binary" in the body tab and selecting the image file.</p>
  
  <p><a href="https://i.stack.imgur.com/t1Dbj.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/t1Dbj.png" alt="Sending on postman"></a></p>
  
  <p>The file is there as intended:</p>
  
  <p><a href="https://i.stack.imgur.com/bAf7W.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/bAf7W.png" alt="Nextcloud server"></a></p>
  
  <p>Moving to implement this on the Flutter app I'm using the code below:</p>
  
  <p>_imageFile = Contains the image from camera or gallery.</p>
  
  <pre class="lang-dart prettyprint-override"><code>RaisedButton(
    color: Colors.blueAccent,
    child: Text('enviar'),
    onPressed: () async {
      String base64Image = base64Encode(_imageFile.readAsBytesSync());
      print(base64Image);
      var client = http.Client();
      var request = http.Request(
          'PUT', Uri.parse('https://host123.com.br/remote.php/dav/files/82427565709/TOOP.jpeg'));
      request.headers.addAll(
          {HttpHeaders.authorizationHeader: 'Basic ODI0Mjc1NjU3MDk6Y2xlYW5uZXQ=', 'Content-Type': 'image/jpeg'});
      request.body = base64Image;
  var streamedResponse = await client.send(request);
      client.close();
      print('ok!');
    },
  ),
  </code></pre>
  
  <p>The result on terminal:</p>
  
  <pre><code>I/flutter ( 8349): /9j/4AAQSkZJRgABAQEASABIAAD/4gxYSUNDX1BST0ZJTEUAAQEAAAxITGlubwIQAABtbnRyUkdCIFhZWiAHzgACAAkABgAxAABhY3NwTVNGVAAAAABJRUMgc1JHQgAAAAAAAAAAAAAAAAAA9tYAAQAAAADTLUhQICAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABFjcHJ0AAABUAAAADNkZXNjAAABhAAAAGx3dHB0AAAB8AAAABRia3B0AAACBAAAABRyWFlaAAACGAAAABRnWFlaAAACLAAAABRiWFlaAAACQAAAABRkbW5kAAACVAAAAHBkbWRkAAACxAAAAIh2dWVkAAADTAAAAIZ2aWV3AAAD1AAAACRsdW1pAAAD+AAAABRtZWFzAAAEDAAAACR0ZWNoAAAEMAAAAAxyVFJDAAAEPAAACAxnVFJDAAAEPAAACAxiVFJDAAAEPAAACAx0ZXh0AAAAAENvcHlyaWdodCAoYykgMTk5OCBIZXdsZXR0LVBhY2thcmQgQ29tcGFueQAAZGVzYwAAAAAAAAASc1JHQiBJRUM2MTk2Ni0yLjEAAAAAAAAAAAAAABJzUkdCIElFQzYxOTY2LTIuMQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAWFlaIAAAAAAAAPNRAAEAAAABFsxYWVogAAAAAAAAAAAAAAAAAAAAAFhZWiAAAAAAAABvogAAOPUAAAOQWFlaIAAAAAAAAGKZAAC3hQAAGNpYWVogAAAAAAAAJKAAAA+EAAC2z2Rlc2MAAAAAAAAAFklFQyBodHRwOi8vd3d3LmllYy5jaAAAAAAAAAAAAAAAFklFQyBodHRwOi8vd3d3LmllYy5jaAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABkZXNjAAAAAAAAAC5JRUMgNjE5NjY
  I/flutter ( 8349): ok
  </code></pre>
  
  <p>The result on the Nextcloud server sending via APP:</p>
  
  <p><a href="https://i.stack.imgur.com/lI0o1.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/lI0o1.png" alt="Nextcloud server 2"></a></p>
  
  <p>Looks like there is something wrong with the encoding or the data that's being sent on the body. The file get "corrupted"</p>
  
  <p>It is the same file on both tests.</p>
  
  <p>** UPDATE **</p>
  
  <p>After changing the body data to just the bytes, as suggested, without base64encoding. (Image Below)</p>
  
  <p><a href="https://i.stack.imgur.com/IVfDk.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/IVfDk.png" alt="Just bytes"></a></p>
  
  <p>Tried convert using .toString() method too, the file went to the server but stayed the same as before, looking corrupted.</p>