Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add cookies to Undertow's ClientRequest?

Tags:

java

undertow

final ClientRequest request = new ClientRequest();
request.setMethod(new HttpString(requestMethod));
                    request.getRequestHeaders().put(Headers.TRANSFER_ENCODING, "chunked");
connection.sendRequest(request, new ClientCallback<ClientExchange>() {
    @Override
    public void completed(ClientExchange clientExchange){}
    @Override
    public void failed(IOException e){}
}

I am following the example for the Undertow Client API. How do I add cookies to the request?

like image 958
theAnonymous Avatar asked Apr 05 '19 00:04

theAnonymous


People also ask

Can I set my own cookies for ihttpclientfactory?

Note if propagation of the request cookies is not what you need (sorry Op) you can set your own cookies when configuring the client factory. Inject the IHttpClientFactory into your controller or middleware.

What is requestdumpinghandler in Undertow?

Undertow is a lightweight open-source Java web server, and the default web server in WildFly. It comes with several handlers, one of which is RequestDumpingHandler for logging HTTP exchanges. # Script for activating request logging based on Undertow's RequestDumpingHandler on WildFly.

How do I enable cookies in Chrome Developer Tools?

To open up Developer Tools in Chrome, you can hold down CTRL + Shift + I on your keyboard or you can manually navigate to it in the “More tools” menu: A screenshot showing the location of Developer Tools in Chrome. The location of the Application tab in Developer Tools. Select the Cookies menu.

Why getheaders() method does not work with cookies?

Note, that despite cookies have a separate getCookies () method, they are just http headers in the end, so the getHeaders () method won't return your modified cookie list in the Cookie header. If you want getHeaders () to work, you have to override that method as well.


1 Answers

Cookies are stored in the request headers. Therefore you can do that:

final ClientRequest request = new ClientRequest();
request.setMethod(new HttpString(requestMethod));
request.getRequestHeaders().put(Headers.TRANSFER_ENCODING, "chunked");
request.getRequestHeaders().put(Headers.COOKIE, "name=value; name2=value2; name3=value3");

connection.sendRequest(request, new ClientCallback<ClientExchange>() {
    @Override
    public void completed(ClientExchange clientExchange){}
    @Override
    public void failed(IOException e){}
}
like image 109
devgianlu Avatar answered Oct 09 '22 12:10

devgianlu