0

I sent a HTTP POST request using HttpURLConnection. Could someone tell me how to format the array parameters and send them. Here are the request Body parameters in JSON.

{
  "data": [
    {
      "type": "enrolled-courses",
      "id": "ea210647-aa59-49c1-85d1-5cae0ea6eed0"
    }
  ]
}

Currently this is my Code.

 private void sendPost() throws Exception {


             URL url = new URL("my_url");
                Map<String,Object> params = new LinkedHashMap<>();
                params.put("type", "courses");
                params.put("id", "19cfa424-b215-4c39-ac13-0491f1a5415d");           

                StringBuilder postData = new StringBuilder();
                for (Map.Entry<String,Object> param : params.entrySet()) {
                    if (postData.length() != 0) postData.append('&');
                    postData.append(URLEncoder.encode(param.getKey(), "UTF-8"));
                    postData.append('=');
                    postData.append(URLEncoder.encode(String.valueOf(param.getValue()), "UTF-8"));
                }
                byte[] postDataBytes = postData.toString().getBytes("UTF-8");

                HttpURLConnection conn = (HttpURLConnection)url.openConnection();
                conn.setRequestMethod("POST");
                conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                conn.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
                conn.setDoOutput(true);
                conn.getOutputStream().write(postDataBytes);

                Reader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));

                for (int c; (c = in.read()) >= 0;)
                    System.out.print((char)c);
            }

The response Code is 204 which means no content. I would appreciate any help.

skghoolala
  • 23
  • 7
  • Possible duplicate of [Java - sending HTTP parameters via POST method easily](https://stackoverflow.com/questions/4205980/java-sending-http-parameters-via-post-method-easily) – DamCx Sep 01 '17 at 09:49
  • Make Object as per your requirement, then convert it into string using Jackson's Object Mapper. – utsav anand Sep 01 '17 at 12:16

1 Answers1

0

I recomend try http-request built on apache http api.

private static final HttpRequest<String.class> HTTP_REQUEST = 
      HttpRequestBuilder.createPost(YOUR_URI, String.class)
           .responseDeserializer(ResponseDeserializer.ignorableDeserializer())
           .build();

private void sendPost(){
      Map<String, String> params = new HashMap<>();
      params.put("type", "courses");
      params.put("id", "19cfa424-b215-4c39-ac13-0491f1a5415d"); 

      ResponseHandler<String> rh = HTTP_REQUEST.execute(params);
      rh.ifSuccess(this::whenSuccess).otherwise(this::whenNotSuccess);
}

privat void whenSuccess(ResponseHandler<String> rh){
    if(rh.hasContent()){
       System.out.println(rh.get()); // prints response
    }else{
       System.out.println("Response is not available. Status code is: " + rh.getStatusCode());
    }
}

privat void whenNotSuccess(ResponseHandler<String> rh){
  System.out.println(rh.getErrorText());
}
Beno
  • 945
  • 11
  • 22