0

There is one POST API in Azkaban to upload a zip file. I am able to upload by using curl as they have given in documentation.

curl -k -i -H "Content-Type: multipart/mixed" -X POST --form 'session.id=47cb9240-f8fe-46f9-9cba-1c1a293a0cf3' --form 'ajax=upload' --form 'file=@atest.zip;type=application/zip' --form 'project=aaaa;type=plain' http://localhost:8081/manager

http://azkaban.github.io/azkaban/docs/2.5/#api-upload-a-project-zip

But I want to call the same API in Java. Can somebody help me how to do it in Java?

Jens
  • 67,715
  • 15
  • 98
  • 113
hatellla
  • 4,796
  • 8
  • 49
  • 101
  • This might help you http://stackoverflow.com/questions/6917105/java-http-client-to-upload-file-over-post – Upio Apr 10 '15 at 06:38

1 Answers1

1

You will need to use Apache HttpComponents framework.

Create a HttpClient, a HttpPost request, and a multipart entity, and then execute the Request. A sample below:

HttpClient httpClient = HttpClientBuilder.create().setDefaultConnectionConfig(config).build();
HttpPost httpPost = new HttpPost(url);

MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);

// add parts to your form here
builder.addPart("<param name>", <part>);

// if you need to upload a file
File uploadPkg = new File(pathToUpload);
FileBody fBody = new FileBody(uploadPkg);
builder.addPart("file", fBody);

HttpEntity entity = builder.build();
httpPost.setEntity(entity);

HttpResponse httpResponse =  httpClient.execute(httpPost);

System.out.println(EntityUtils.toString(httpResponse.getEntity()));

Hope it helps.

inquizitive
  • 622
  • 4
  • 21