Sample code for 30+ languages & platforms
Java

POST application/x-www-form-urlencoded using REST API

See more REST Examples

Demonstrates how to send a POST with query params (x-www-form-urlencoded) using the Chilkat REST object.

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Rest;

public class ChilkatExample {

    public static void main(String[] args) {
        // This example requires the Chilkat API to have been previously unlocked.
        // See Global Unlock Sample for sample code.

        Rest rest = new Rest();

        // This example will send to https://www.chilkatsoft.com/echoPost.asp

        // Make the initial connection (without sending a request yet).
        boolean bTls = true;
        int port = 443;
        boolean bAutoReconnect = true;
        String responseStr;
        try {
            rest.connect("www.chilkatsoft.com", port, bTls, bAutoReconnect);

            // Provide query params.
            rest.addQueryParam("firstName", "John");
            rest.addQueryParam("lastName", "Doe");
            rest.addQueryParam("company", "Bisco Bits Ltd.");

            responseStr = rest.fullRequestFormUrlEncoded("POST", "/echoPost.asp");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        // When successful, the response status code will equal 200.
        if (rest.getResponseStatusCode() != 200) {
            // Examine the request/response to see what happened.
            System.out.println("response status code = " + rest.getResponseStatusCode());
            System.out.println("response status text = " + rest.getResponseStatusText());
            System.out.println("response header: " + rest.getResponseHeader());
            System.out.println("response body (if any): " + responseStr);
            System.out.println("---");
            System.out.println("LastRequestStartLine: " + rest.getLastRequestStartLine());
            System.out.println("LastRequestHeader: " + rest.getLastRequestHeader());
            return;
        }

        System.out.println(responseStr);
        System.out.println("Success.");
    }
}