Sample code for 30+ languages & platforms
Dart

MercadoLibre - Cargar factura (Upload Invoice)

See more MercadoLibre Examples

Cargar factura en detalle de venta

Chilkat Dart Downloads

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

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

  final http = CkHttp();

  // First get our previously obtained OAuth2 access token.
  final jsonToken = CkJsonObject();
  jsonToken.loadFile('qa_data/tokens/mercadolibre.json');

  // Implements the following CURL command:

  // curl -X POST https://api.mercadolibre.com/packs/$PACK_ID/fiscal_documents?access_token=$ACCESS_TOKEN 
  //   -H 'content-type: multipart/form-data;' 
  //   -F 'fiscal_document=@/home/user/.../Factura_adjunta.pdf'

  // Use the following online tool to generate HTTP code from a CURL command
  // Convert a cURL Command to HTTP Source Code

  final sbPath = CkStringBuilder();
  sbPath.append('/packs/\$PACK_ID/fiscal_documents?access_token=\$ACCESS_TOKEN');
  // Use an actual pack ID.
  var numReplaced = sbPath.replace('\$PACK_ID', '2000000089077943');
  numReplaced = sbPath.replace('\$ACCESS_TOKEN', jsonToken.stringOf('access_token'));

  final req = CkHttpRequest();
  req.httpVerb = 'POST';
  req.path = sbPath.getAsString();
  req.contentType = 'multipart/form-data';
  req.addFileForUpload2('fiscal_document', '/home/user/.../Factura_adjunta.pdf', 'application/pdf');

  req.addHeader('Expect', '100-continue');
  req.addHeader('content-type', 'multipart/form-data;');

  final resp = CkHttpResponse();
  try {
    http.httpSReq('api.mercadolibre.com', 443, true, req, resp);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  final sbResponseBody = CkStringBuilder();
  resp.getBodySb(sbResponseBody);
  final jResp = CkJsonObject();
  jResp.loadSb(sbResponseBody);
  jResp.emitCompact = false;

  print('Response Body:');
  print(jResp.emit());

  final respStatusCode = resp.statusCode;
  print('Response Status Code = $respStatusCode');
  if (respStatusCode >= 400) {
    print('Response Header:');
    print(resp.header);
    print('Failed.');
    return;
  }

  // Sample JSON response:
  // (Sample code for parsing the JSON response is shown below)

  // {
  //   "ids": [
  //     "415460047_a96d8dea-38cd-4402-938e-80a1c134fc5d"
  //   ]
  // }

  // Sample code for parsing the JSON response...
  // Use the following online tool to generate parsing code from sample JSON:
  // Generate Parsing Code from JSON

  var strVal = '';

  var i = 0;
  final countI = jResp.sizeOfArray('ids');
  while (i < countI) {
    jResp.i = i;
    strVal = jResp.stringOf('ids[i]');
    i++;
  }
}