6

How do I add my own headers to a request wrapped by ClientResource in Restlet? For example, I've read that you can use the following when working directly with Client:

Form headers = (Form) request.getAttributes().get(HeaderConstants.ATTRIBUTE_HEADERS);
if (headers == null) {
 headers = new Form();
 request.getAttributes().put("org.restlet.http.headers", responseHeaders);
}
headers.add("X-Some-Header", "the value");

However, I am basically following the code provided in their tutorial and I do not know which member of ClientResource should be accessed to set headers:

ClientResource clientResource = new ClientResource("http://webserviceurl");

MyClassResource classResource = clientResource.wrap(classResource.class);

MyClass class;

try { class = resource.retrieve(); } catch (Exception e) { System.out.println("fail."); }

What can I do to modify retrieve() to add some headers?

tacos_tacos_tacos
  • 10,277
  • 11
  • 73
  • 126

3 Answers3

13

The ClientResource method has a getRequestAttributes method which is a shortcut for: getRequest().getAttributes().

So you can use it in order to specify your custom headers for the request, as described below:

ClientResource cr = new ClientResource("...");
Series<Header> headers = cr.getRequestAttributes().get(
                                 "org.restlet.http.headers");
headers.set("<header-name>", "<header-value>");

Be aware that most of headers are managed by Restlet by default. To see which headers are supported, have a look at the HeaderUtils class: https://github.com/restlet/restlet-framework-java/blob/master/modules/org.restlet/src/org/restlet/engine/header/HeaderUtils.java.

Edited

With latest versions of Restlet (2.3), a method getHeaders was added:

ClientResource cr = new ClientResource("...");
Series<Header> headers = cr.getHeaders();
headers.set("<header-name>", "<header-value>");

This corresponds to custom headers.

Hope it will help you. Thierry

Thierry Templier
  • 198,364
  • 44
  • 396
  • 360
  • i'm get the follow error with this code: Type mismatch: cannot convert from Object to Series
    – ademar111190 May 22 '12 at 21:51
  • 4
    With version 2.1.2: Series
    headers = (Series
    ) res.getRequestAttributes().get("org.restlet.http.headers"); if (headers == null) { headers = new Series
    (Header.class); } headers.add("user", "abcd1"); headers.add("password", "welcome");
    – Ashwin Jayaprakash May 30 '13 at 18:13
  • @thierry-templier sorry to inject into this conversation but can you help me out on this http://stackoverflow.com/questions/35383763/how-to-get-mediatype-from-request – quarks Feb 15 '16 at 03:41
3

If you're using restlet 2.0.x (the latest stable version), you need to do this:

ClientResource resource = new ClientResource(yourUrl);
Form headers = (Form)resource.getRequestAttributes().get("org.restlet.http.headers");
if (headers == null) {
    headers = new Form();
    resource.getRequestAttributes().put("org.restlet.http.headers", headers);
}
headers.add("yourHeaderName", yourHeaderValue);
resource.get();
Response response = resource.getResponse();
String text = response.getEntity().getText();
String status = response.getStatus().toString();
crizCraig
  • 8,487
  • 6
  • 54
  • 53
1

This worked for me, so I'm sharing this.

        ClientResource client = new ClientResource(uri);

        Series<Header> headerValue = new Series<>(Header.class);
        Request request = client.getRequest();
        headerValue.add("header name", "header value");
        request.getAttributes().put(HeaderConstants.ATTRIBUTE_HEADERS, headerValue);
Ravipati Praveen
  • 446
  • 1
  • 7
  • 28