javarestlet-2.0

Getting the full HTTP Accept header string in restlet?


Apologies for the newbie question, but I am struggling a way to get the full contents of the HTTP Accept header received from the client web browser out of restlet (v 2.3.5) ?

This :

this.request.getHeaders().getFirstValue("Accept",true);

Does not work, I only get */* back.

I have also tried :

        List<Preference<MediaType>> mediaTypes = this.request.getClientInfo().getAcceptedMediaTypes();
    this.logger.debug(mediaTypes.toString());
    for (Iterator it = mediaTypes.iterator();it.hasNext();) {
        Preference<MediaType> preference = (Preference<MediaType>) it.next();
        this.logger.debug(preference.toString());
    }

Again, this only returns [*/*:1.0] and */*:1.0 respectively.


Solution

  • In fact Restlet provides an object representation of elements present in the request. Regarding the Accept header, you have the following rules based on the MediaType class :

    If you want to know the corresponding value of the header would be:

    Accept: media-type-value;q=quality,media-type-value;q=quality,media-type-value;q=quality
    

    For example, if you get the following values using the code getClientInfo().getAcceptedMediaTypes().toString():

    [text/html:1.0, application/xhtml+xml:1.0, application/xml:0.9, image/webp:1.0, */*:0.8]
    

    The corresponding header would be:

    Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
    

    Hope it helps you, Thierry