Skip to content

add a ClientResource.get(Object entity, Class<T> resultClass) instance method #1520

Description

@dlaprova

According to RFC 9110:

A client SHOULD NOT generate content in a GET request unless it is made directly to an origin server that has previously indicated, in or out of band, that such a request has a purpose and will be adequately supported.

I unfortunately find myself in the case where the server accepts only GET requests to an endpoint where some parameters are expected to be in the body of the request.

Since adding a body in the GET request is not strictly forbidden by the specification, it would be nice if there was an overloaded ClientResource.get() method accepting an entity object as the body of the request, in a similar fashion as it is done with POST requests.

As a workaround, I set the body of the request by looking at how the ClientResource does it when sending POST requests:

/**
 * Helper method to set a body in a request of a ClientResource, in the same way Restlet does when setting POST
 * request body contents.
 * @param clientResource
 * @param entity
 * @param <T>
 */
protected <T> void _setBody(ClientResource clientResource, T entity){

    ConverterService cs = clientResource.getConverterService();
    Request request = clientResource.getRequest();
    if (entity != null) {
        List<? extends Variant> entityVariants;
        try {
            entityVariants = cs.getVariants(entity.getClass(), null);
            request.setEntity(clientResource.toRepresentation(entity,
                clientResource.getConnegService().getPreferredVariant(entityVariants,
                    request, clientResource.getMetadataService())));
        } catch (IOException e) {
            throw new ResourceException(e);
        }
    } else {
        request.setEntity(null);
    }
}

// usage
_setBody(clientResource, requestDTO);
return clientResource.get(representationClass)

Is this the intended way to deal with such situations?

Thank you for maintaning the Restlet framework.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions