Send csv file to a api end point using HttpPost

29 Views Asked by At

This is the code i am using to send a .csv file to an api end point which expects a csv file.

  csvfile=new File("/home/bpidathala/Music/Import.csv");
        String importurl =CMnEnv.getEnv().getConf().getString("com.modeln.AppSwitch.uam.import.url");
        HttpPost httpPost = new HttpPost(importurl);
        // Set additional request headers dynamically
        httpPost.setHeader("Content-Type", "multipart/form-data");
        if (headers != null && !headers.isEmpty()) {
            for (Map.Entry<String, String> header : headers.entrySet()) {
                httpPost.setHeader(header.getKey(), header.getValue());
            }
        }
        // Set the request body, if provided
        if (csvfile != null && csvfile.length()>0) {
            FileBody fileBody = new FileBody(csvfile, ContentType.DEFAULT_BINARY);
            MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
            entityBuilder.addPart("file", fileBody);
            HttpEntity entity = entityBuilder.build();
            httpPost.setEntity(entity);
        }

        HttpResponse httpResponse = httpClient.execute(httpPost);
        int responseCode = httpResponse.getStatusLine().getStatusCode();

this code is not working, i am getting 500 repsonse code but when tried through post man it is working fine

i tired adding a .csv file to the request body.

i need to make this api call by sending a csv file over http post

0

There are 0 best solutions below