Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid sending Cookie header in java unirest requests?

I noticed that using unirest java library cookies are by default sent in requests after being set in responses (just like any browser does). Is there any way to avoid it?

Example:

public class Main {
    private static HttpResponse<JsonNode> doRequest() throws UnirestException {
        try {
            HttpResponse<JsonNode> jsonResponse = Unirest
                    .get("http://example.com")
                    .header("Accept", "application/json").asJson();
            return jsonResponse;
        } catch (UnirestException e) {
            throw e;
        }

    }
    public static void main(String[] args) throws UnirestException {
        //first request receive a set-cookie header in response
        doRequest();
        //second request send a Cookie header with the cookie set by the first one: can I avoid this?
        doRequest();
    }
}
like image 806
Zac Avatar asked Mar 15 '23 20:03

Zac


2 Answers

It is probably due to a default setting on the underlying HttpClient implementation. Setting a custom HttpClient seems to work:

HttpClient httpClient = HttpClients.custom()
    .disableCookieManagement()
    .build();
Unirest.setHttpClient(httpClient);
like image 53
Zac Avatar answered Mar 25 '23 05:03

Zac


Looks like

Unirest.config().enableCookieManagement(false);

solves the problem.

like image 30
Lu55 Avatar answered Mar 25 '23 03:03

Lu55