Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Attaching query parameters in a ClientRequestFilter

I simply need to attach query parameters onto an outgoing request. (Java EE 7.0, JAX-RS 2.0)

In specifics, I currently using the RESTeasy Client ver 3.0.14, so I make my calls using the fancy interface-proxy system. I was attempting to produce something like this:

myapplication/api/path?timestamp=000

with:

@Provider
public class MyRequestFilter implements ClientRequestFilter {

    @Context
    private HttpServletRequest servletRequest;

    public void filter(ClientRequestContext requestContext) throws IOException {

        servletRequest.getParameterMap().put("timestamp", new String[]{
                String.valueOf(new Date().getTime())
        });

    }
}

I made sure I was registering it with client.register(MyRequestFilter.class) as well. Feel free to ask questions. Thanks!

like image 517
Kevin Thorne Avatar asked Jun 21 '16 21:06

Kevin Thorne


People also ask

How can I append a query parameter to an existing URL?

append() The append() method of the URLSearchParams interface appends a specified key/value pair as a new search parameter. As shown in the example below, if the same key is appended multiple times it will appear in the parameter string multiple times for each value.

How do you append a parameter query?

Query parameters are a defined set of parameters attached to the end of a url. They are extensions of the URL that are used to help define specific content or actions based on the data being passed. To append query params to the end of a URL, a '? ' Is added followed immediately by a query parameter.


1 Answers

Credit to @peeskillet --

Rebuild the URI from the requestContext like this:

requestContext.setUri(UriBuilder.fromUri(requestContext.getUri()).queryParam("key", value).build());

You can now see the new query parameter with

requestContext.getUri().toString();

Again, verify that you register it when making the REST Client

client.register(MyRequestFilter.class);
like image 101
Kevin Thorne Avatar answered Sep 30 '22 02:09

Kevin Thorne