Sample code for 30+ languages & platforms
Dart

REST File Upload (multipart/form-data)

See more REST Examples

Demonstrates how to upload a file using multipart/form-data.

Chilkat Dart Downloads

Dart
import 'package:chilkat/chilkat.dart';

void main() {
  // This example requires the Chilkat API to have been previously unlocked.
  // See Global Unlock Sample for sample code.

  final rest = CkRest();

  // Connect to the HTTP server using TLS.
  final bTls = true;
  final port = 443;
  final bAutoReconnect = true;
  // Make sure to replace "www.chilkatsoft.com" with your domain..
  try {
    rest.connect('www.chilkatsoft.com', port, bTls, bAutoReconnect);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  final fileStream = CkStream();
  fileStream.sourceFile = 'qa_data/jpg/starfish.jpg';

  rest.addHeader('Content-Type', 'multipart/form-data');

  rest.partSelector = '1';
  rest.addHeader('Content-Type', 'image/jpg');
  rest.addHeader('Content-Disposition', 'form-data; name="filename"; filename="starfish.jpg"');
  rest.setMultipartBodyStream(fileStream);
  rest.partSelector = '0';

  final String responseBody;
  try {
    responseBody = rest.fullRequestMultipart('POST', '/the_uri_path_to_receive_the_upload');
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  if (rest.responseStatusCode != 200) {
    print('Received error response code: ${rest.responseStatusCode}');
    return;
  }

  print('File uploaded');
}