Post Request Using Application/X-Www-Form-Urlencoded

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

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

String urlParameters  = "param1=data1¶m2=data2¶m3=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.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("charset", "utf-8");
conn.setRequestProperty("Content-Length", Integer.toString(postDataLength ));
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;
result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
return result.toString();

POST Request in Body as x-www-form-urlencoded

You are mixing things up. Those parameters grant_type, client_id, client_secret and refresh_token are not HTTP-Headers but the payload that you're sending to the server.

According to the docs (, this should work:

var cisurl = "";

var data = {
'grant_type': 'refresh_token',
'client_id': 'abcdefg',
'client_secret': 'hijklmn',
'refresh_token': 'opqrstuvw'

var options = {
'method': 'post',
'payload': data

var response = UrlFetchApp.fetch(cisurl,options);

P.S.: Do not share your secrets on stack-overflow

POST request with Content-Type application/x-www-form-urlencoded and

Exactly what I needed.
Java HTTP POST request with HttpURLConnection section on the page did the work.

PYTHON: how to send request_body encoded as application/x-www-form-urlencoded

You can specify the request type in the request header.

    headers = {'Content-Type': 'application/x-www-form-urlencoded'}
response =, data=request_body, headers=headers)

application/x-www-form-urlencoded or multipart/form-data?


Summary; if you have binary (non-alphanumeric) data (or a significantly sized payload) to transmit, use multipart/form-data. Otherwise, use application/x-www-form-urlencoded.

The MIME types you mention are the two Content-Type headers for HTTP POST requests that user-agents (browsers) must support. The purpose of both of those types of requests is to send a list of name/value pairs to the server. Depending on the type and amount of data being transmitted, one of the methods will be more efficient than the other. To understand why, you have to look at what each is doing under the covers.

For application/x-www-form-urlencoded, the body of the HTTP message sent to the server is essentially one giant query string -- name/value pairs are separated by the ampersand (&), and names are separated from values by the equals symbol (=). An example of this would be: 


According to the specification:

[Reserved and] non-alphanumeric characters are replaced by `%HH', a percent sign and two hexadecimal digits representing the ASCII code of the character

That means that for each non-alphanumeric byte that exists in one of our values, it's going to take three bytes to represent it. For large binary files, tripling the payload is going to be highly inefficient.

That's where multipart/form-data comes in. With this method of transmitting name/value pairs, each pair is represented as a "part" in a MIME message (as described by other answers). Parts are separated by a particular string boundary (chosen specifically so that this boundary string does not occur in any of the "value" payloads). Each part has its own set of MIME headers like Content-Type, and particularly Content-Disposition, which can give each part its "name." The value piece of each name/value pair is the payload of each part of the MIME message. The MIME spec gives us more options when representing the value payload -- we can choose a more efficient encoding of binary data to save bandwidth (e.g. base 64 or even raw binary).

Why not use multipart/form-data all the time? For short alphanumeric values (like most web forms), the overhead of adding all of the MIME headers is going to significantly outweigh any savings from more efficient binary encoding.

Related Topics

Leave a reply