Sample code for 30+ languages & platforms
React Native

MercadoLibre - Cargar factura (Upload Invoice)

See more MercadoLibre Examples

Cargar factura en detalle de venta

Chilkat React Native Downloads

React Native
import { Http, HttpRequest, HttpResponse, JsonObject, StringBuilder } from '@chilkat/react-native'

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

  const http = new Http();

  // First get our previously obtained OAuth2 access token.
  const jsonToken = new JsonObject();
  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

  const sbPath = new StringBuilder();
  sbPath.append('/packs/$PACK_ID/fiscal_documents?access_token=$ACCESS_TOKEN');
  // Use an actual pack ID.
  let numReplaced = sbPath.replace('$PACK_ID', '2000000089077943');
  numReplaced = sbPath.replace('$ACCESS_TOKEN', jsonToken.stringOf('access_token'));

  const req = new HttpRequest();
  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;');

  const resp = new HttpResponse();
  try {
    await http.httpSReqAsync('api.mercadolibre.com', 443, true, req, resp);
  } catch {
    console.log(http.lastErrorText);
    return;
  }

  const sbResponseBody = new StringBuilder();
  resp.getBodySb(sbResponseBody);
  const jResp = new JsonObject();
  jResp.loadSb(sbResponseBody);
  jResp.emitCompact = false;

  console.log('Response Body:');
  console.log(jResp.emit());

  const respStatusCode = resp.statusCode;
  console.log(`Response Status Code = ${respStatusCode}`);
  if (respStatusCode >= 400) {
    console.log('Response Header:');
    console.log(resp.header);
    console.log('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

  let strVal = '';

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