4

I have a dropwizard service in whitch i implemented a post request who consumes APPLICATION_FORM_URLENCODED media type and uses @FormParam annotation

Then in my client i'm using Apache HttpClient to make a post request like this:

public void sendPost(String path, JsonObject params) throws Exception {

                String url = "http://" + TS_API_HOST + ":" + TS_API_PORT + "/" + path;

                CloseableHttpClient httpClient = HttpClients.createDefault();
                HttpPost httpPost = new HttpPost(url);
                httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
                List<NameValuePair> nvps = new ArrayList<NameValuePair>();

                Iterator<String> keys = params.keySet().iterator();
                while(keys.hasNext()){
                    String currentKey = keys.next();
                    nvps.add(new BasicNameValuePair(currentKey, params.get(currentKey).toString()));
                }
                System.out.println(nvps.toString());
                httpPost.setEntity(new UrlEncodedFormEntity(nvps));

                CloseableHttpResponse response = httpClient.execute(httpPost);

                try {
                    System.out.println(response.getStatusLine());
                    HttpEntity entity2 = response.getEntity();
                    // do something useful with the response body
                    // and ensure it is fully consumed
                    EntityUtils.consume(entity2);
                } finally {
                    response.close();
                }

            }

The url and params I'm passing are correct but i keep getting 400 bad request as a response.

In Postman it works very well...

kitsuneFox
  • 1,243
  • 3
  • 18
  • 31

0 Answers0