Sample code for 30+ languages & platforms
Java

MercadoLibre - Cargar factura (Upload Invoice)

See more MercadoLibre Examples

Cargar factura en detalle de venta

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Http;
import com.chilkatsoft.chilkat2.HttpRequest;
import com.chilkatsoft.chilkat2.HttpResponse;
import com.chilkatsoft.chilkat2.JsonObject;
import com.chilkatsoft.chilkat2.StringBuilder;

public class ChilkatExample {

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

        Http http = new Http();

        // First get our previously obtained OAuth2 access token.
        JsonObject 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

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

        HttpRequest req = new HttpRequest();
        req.setHttpVerb("POST");
        req.setPath(sbPath.getAsString());
        req.setContentType("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;");

        HttpResponse resp = new HttpResponse();
        try {
            http.httpSReq("api.mercadolibre.com", 443, true, req, resp);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

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

        System.out.println("Response Body:");
        System.out.println(jResp.emit());

        int respStatusCode = resp.getStatusCode();
        System.out.println("Response Status Code = " + respStatusCode);
        if (respStatusCode >= 400) {
            System.out.println("Response Header:");
            System.out.println(resp.getHeader());
            System.out.println("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

        String strVal = "";

        int i = 0;
        int count_i = jResp.sizeOfArray("ids");
        while (i < count_i) {
            jResp.setI(i);
            strVal = jResp.stringOf("ids[i]");
            i = i + 1;
        }
    }
}