Search code examples
restlet

Does restlet add a user agent header by default?


Does the restlet java library add a user-agent header if the developer does not specify one?

If so, what is the value does it use for the header?


Solution

  • The content of the User-Agent header is available from the agent attribute of the ClientInfo class:

    // Client side
    getRequest().getClientInfo().setAgent("something");
    
    // Server side
    String userAgent = getRequest().getClientInfo().getAgent();
    

    This can be set on the client side and gotten on the server side.

    If nothing is specified when sending a request with Restlet. For example, with such code:

    String url = "http://localhost:8182/contacts/";
    ClientResource cr = new ClientResource(url);
    cr.get();
    

    The content of the header is the following:

    Jetty/9.2.6.v20141205,Restlet-Framework/2.3.1
    

    In my case, I used Restlet 2.3.1 with the Jetty extension for the client connector (to actually send the request).

    If you set a value on the client side, as described below:

    String url = "http://localhost:8182/contacts/";
    ClientResource cr = new ClientResource(url);
    cr.getClientInfo().setAgent("My user agent");
    cr.get();
    

    You will get now this value on the server side:

    Jetty/9.2.6.v20141205,My user agent
    

    Hope it helps you, Thierry