Sample code for 30+ languages & platforms
Java

Add a Request Header to a REST Request

See more REST Examples

Demonstrates Rest.AddHeader, which adds an HTTP request header field. The 1st argument is the field name and the 2nd is the value. Adding a header whose name already exists replaces the previous value.

Background. Request headers configured with AddHeader are applied to subsequent requests sent through the object, such as Accept, Content-Type, or custom application headers.

Chilkat Java Downloads

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

public class ChilkatExample {

    public static void main(String[] args) {
        Rest rest = new Rest();

        // Connect to the REST server.  The 3rd argument enables TLS (use true for HTTPS on port 443),
        // and the 4th enables automatic reconnection if the connection is dropped between requests.
        boolean bTls = true;
        boolean bAutoReconnect = true;
        String responseText;
        try {
            rest.connect("example.com", 443, bTls, bAutoReconnect);

            // Add request header fields.  The 1st argument is the field name and the 2nd is the value.
            // Adding a header with a name that already exists replaces the previous value.
            rest.addHeader("Accept", "application/json");
            rest.addHeader("X-Custom-Header", "example-value");

            responseText = rest.fullRequestNoBody("GET", "/api/data");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        System.out.println(responseText);
    }
}