skip to Main Content

the procedure of the problem:

  1. I try to get response by sending a form-data, here is the result and details from Postman(successfully got data in Postman):

  2. there is an error thrown:** "<faultstring>the request was rejected because no multipart boundary was found</faultstring>"**

details of the problem:

this is the request header

this is the Postman request details

this is the request body

this is the request body

related method in HttpUtil.java below:

public static String PostFormdata(String url, Map<String, String> map) {
    MultipartEntityBuilder builder = MultipartEntityBuilder.create();

    for(String key:map.keySet()){
        builder.addTextBody(key, map.get(key));
    }
    String result = null;
    try {
        HttpPost httpPost = new HttpPost(url);
        HttpEntity multipart = builder.build();
        httpPost.setHeader("Content-Type", "multipart/form-data");
        httpPost.setHeader("Accept", "application/json");
        httpPost.setConfig(requestConfig);
        httpPost.setEntity(multipart);
        CloseableHttpResponse response = httpClient.execute(httpPost);

        HttpEntity httpEntity = response.getEntity();
        result = EntityUtils.toString(httpEntity, "UTF-8");
    } catch (IOException e) {
        e.printStackTrace();
    }
    return result;
}

here is my usage:

Map<String, String> params = new HashMap<String, String>();

params.put("username", "user01");
params.put("password", "abcd");
params.put("entryid", "111");
params.put("beginDate", "2023-08-01");
params.put("endDate", "2023-10-06");

Map<String, String> param = new HashMap<String, String>();
param.put("params", params.toString());

String result = HttpClientUtils.PostFormdata(url, param);

I want to get a correct result from POST request

2

Answers


  1. try to comment out this line:
    httpPost.setHeader("Content-Type", "multipart/form-data");

    Login or Signup to reply.
  2. I feel, we can better redesign the API which you have posted .
    Sending json kind data in header is not correct. It must be a key value pair not a json with a key called params .
    Either send each data in the header or put as json in the body

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search