Uploading a CSV to a REST web service

import org.apache.http.client.methods.*;
import org.apache.http.client.*;
import org.apache.http.impl.client.*;
import org.apache.http.*;
import org.apache.http.entity.mime.*;
import org.apache.http.entity.*;
import java.io.*;

public class API_H01 {
  public static void main (String args[]) {
    try {

      HttpClient client = HttpClientBuilder.create().build();

      File f = new File("/opt/tomcat8/hierarchy.csv");

      MultipartEntityBuilder builder = MultipartEntityBuilder.create();
      HttpPost rq = new HttpPost("https://host/api/hierarchies/imports");
      rq.addHeader("Authorization", "***key***");
      //don't need, as the builder call sets the content type and boundary for us
      //rq.addHeader("Content-Type", "multipart/form-data; charset=ISO-639; boundary=boundary");
      builder.addBinaryBody("file", new FileInputStream(f), ContentType.create("text/csv"), f.getName());
      HttpResponse rsp = client.execute(rq);

      int rc = rsp.getStatusLine().getStatusCode();

      System.out.println("\nResponse Code: " + rsp.getStatusLine().getStatusCode() + "\n");
      BufferedReader rd = new BufferedReader(new InputStreamReader(rsp.getEntity().getContent()));
      String line = "";
      while ((line = rd.readLine()) != null) {
    catch (Exception e) {

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.