简体   繁体   中英

Java code for HTTP post request to send two(certificate and certificateKey) files as multipart-formdata

I need to send two files with post request as multi-part form data using JAVA and I get BAD REQUEST 400 for this code.

RESPONSE I GET -- 
{
  "status" : "BAD_REQUEST",
  "message" : "Failed to parse multipart servlet request; nested exception is java.io.IOException: org.apache.tomcat.util.http.fileupload.FileUploadException: the request was rejected because no multipart boundary was found",
  "errors" : null,
  "timestamp" : "2019-06-20 05:48:31"
}

I used apache HttpComponents for this.

File certFile = new File("/home/vijay/Downloads/facilio.crt");
    FileBody data = new FileBody(certFile);

    File keyFile = new File("/home/vijay/Downloads/facilio-private.key");
    FileBody data2 = new FileBody(keyFile);

    HttpClient httpclient = new DefaultHttpClient();
    String authString = username + ":" + password;
    System.out.println("auth string: " + authString);
    byte[] authEncBytes =Base64.getEncoder().encode(authString.getBytes());
    String authStringEnc = new String(authEncBytes);
    System.out.println("Base64 encoded auth string: " + authStringEnc);
    HttpPost httpPost = new HttpPost("https://api.xxxx/xxx/xxx");
    httpPost.addHeader(HttpHeaders.AUTHORIZATION, "Basic "+authStringEnc);
    // httpPost.addHeader("Accept", "application/json");
    System.out.println(" content type -"+ContentType.MULTIPART_FORM_DATA.getMimeType());
    httpPost.addHeader(HttpHeaders.CONTENT_TYPE,ContentType.MULTIPART_FORM_DATA.getMimeType());



    MultipartEntity reqEntity = new MultipartEntity();
    MultipartEntity reqEntity1 = new MultipartEntity();

    reqEntity.addPart("certificate", data);
    reqEntity1.addPart("ceritificateKey",data2);
    httpPost.setEntity(reqEntity);
    httpPost.setEntity(reqEntity1);
    try {
        HttpResponse response = httpclient.execute(httpPost);
        System.out.println("code-------"+response.getStatusLine().getStatusCode());
        System.out.println(response.toString());
        HttpEntity ent = response.getEntity();
        InputStream is = ent.getContent();
        InputStreamReader isr = new InputStreamReader(is);
        int numCharsRead;
        char[] charArray = new char[1024];
        StringBuffer sb = new StringBuffer();
        while ((numCharsRead = isr.read(charArray)) > 0) {
            sb.append(charArray, 0, numCharsRead);
        }
        String result = sb.toString();

        System.out.println("*** BEGIN ***");
        System.out.println(result);
        System.out.println("*** END ***");

    } catch (IOException e) {
        e.printStackTrace();
    }

I should get 200 with a response String. Any suggestion or code review is greatly appreciated. Thanks in advance

Not sure it's the only issue in your code, but MultipartEntity , as its name implies holds a multipart entity. You need only one for the request:

MultipartEntity reqEntity = new MultipartEntity();

reqEntity.addPart("certificate", data);
reqEntity.addPart("ceritificateKey",data2);
httpPost.setEntity(reqEntity);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM