0

I am getting a Server Error with the code 500 while trying to upload a json file to a server.The problem occurs because of the parameters. When I use a plain json string, without a multipart builder, then it works. Code I use is below

Many thanks in advance

public void uploadSegmentCustomerList(File jSonFile, String segmentId) throws IOException {

    if(this.apiClient.getToken() == null){
        apiClient.login();
    }


    MultipartEntityBuilder
            builder = MultipartEntityBuilder.create();
    builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
    HttpPost uploadPost = new HttpPost(restConfig.getBaseUrl() + restConfig.getUploadUrl().replace("@segmentId@",segmentId));
    uploadPost.setHeader("sessionToken", this.apiClient.getToken());

    // This attaches the file to the POST:

    File f = jSonFile;
    builder.addBinaryBody("test" , new FileInputStream(f), ContentType.APPLICATION_JSON,f.getName() );

    HttpEntity multipart = builder.build();
    uploadPost.setEntity(multipart);

    HttpResponse response = apiClient.uploadPost(uploadPost);
    int responseCode = response.getStatusLine().getStatusCode();

    log.info(String.valueOf(response)+ " "+ responseCode);
}
emrahozkan
  • 193
  • 1
  • 3
  • 15
  • 1
    Did you check what's a 500 error? It's mean internal server error. With just the client code and no more informations (server type, server log, what kind of service...) we can't do anything... – Fefux Nov 10 '16 at 10:38
  • you have to know about the host specs, and why it throws the 500 code. – Incepter Nov 10 '16 at 10:40
  • Fefux, you are right. The problem happens because of the parameters. When I use a plain json string, without a multipart builder, then it works. I will edit the post as well – emrahozkan Nov 10 '16 at 10:46
  • I see you use addBinaryBody. But i think this is for transfer raw file (like images). You need to open your file, read it in a String and add your data with the method addTextBody (See also : http://stackoverflow.com/questions/19292169/multipartentitybuilder-and-charset) – Fefux Nov 10 '16 at 10:55
  • Thank you for the further help. I agree with this solution but somehow the corresponding server expects us to upload a json file – emrahozkan Nov 10 '16 at 11:06
  • Oh ok, so we need more informations about the server and why it crashes with your file. Maybe the server wants some other parameters or with other names than "test" – Fefux Nov 10 '16 at 11:14
  • ok it turns out the server just accepts strings rather than files but we have been told just now. thank you for your help, i will remove the quesiton in a few minutes – emrahozkan Nov 10 '16 at 12:11

0 Answers0