Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send post request with x-www-form-urlencoded body

Tags:

java

Request in postman

How in java, can I send a request with x-www-form-urlencoded header. I don't understand how to send a body with a key-value, like in the above screenshot.

I have tried this code:

String urlParameters =   cafedra_name+ data_to_send; URL url;     HttpURLConnection connection = null;       try {       //Create connection       url = new URL(targetURL);       connection = (HttpURLConnection)url.openConnection();       connection.setRequestMethod("POST");       connection.setRequestProperty("Content-Type",             "application/x-www-form-urlencoded");        connection.setRequestProperty("Content-Length", "" +                 Integer.toString(urlParameters.getBytes().length));       connection.setRequestProperty("Content-Language", "en-US");          connection.setUseCaches (false);       connection.setDoInput(true);       connection.setDoOutput(true);        //Send request       DataOutputStream wr = new DataOutputStream (                   connection.getOutputStream ());       wr.writeBytes (urlParameters);       wr.flush ();       wr.close (); 

But in the response, I don't receive the correct data.

like image 799
mari Avatar asked Nov 13 '16 14:11

mari


People also ask

How do you send POST request with X-www-form-Urlencoded body in Java HttpURLConnection?

String urlParameters = cafedra_name+ data_to_send; URL url; HttpURLConnection connection = null; try { //Create connection url = new URL(targetURL); connection = (HttpURLConnection)url. openConnection(); connection. setRequestMethod("POST"); connection.


1 Answers

As you set application/x-www-form-urlencoded as content type so data sent must be like this format.

String urlParameters  = "param1=data1&param2=data2&param3=data3"; 

Sending part now is quite straightforward.

byte[] postData = urlParameters.getBytes( StandardCharsets.UTF_8 ); int postDataLength = postData.length; String request = "<Url here>"; URL url = new URL( request ); HttpURLConnection conn= (HttpURLConnection) url.openConnection();            conn.setDoOutput(true); conn.setInstanceFollowRedirects(false); conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");  conn.setRequestProperty("charset", "utf-8"); conn.setRequestProperty("Content-Length", Integer.toString(postDataLength )); conn.setUseCaches(false); try(DataOutputStream wr = new DataOutputStream(conn.getOutputStream())) {    wr.write( postData ); } 

Or you can create a generic method to build key value pattern which is required for application/x-www-form-urlencoded.

private String getDataString(HashMap<String, String> params) throws UnsupportedEncodingException{     StringBuilder result = new StringBuilder();     boolean first = true;     for(Map.Entry<String, String> entry : params.entrySet()){         if (first)             first = false;         else             result.append("&");             result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));         result.append("=");         result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));     }         return result.toString(); } 
like image 182
Navoneel Talukdar Avatar answered Sep 28 '22 19:09

Navoneel Talukdar