7

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!

Kevin Thorne
  • 435
  • 5
  • 19
  • The reason I marked it as a duplicate is because you are using the wrong filter. Has nothing to do with query parameters or headers. You are using the server filter, when you should be using the client filter (as explained in the duplicate post) – Paul Samsotha Jun 22 '16 at 14:04
  • If you can't figure out how to do it with the correct filter, please update your post with the attempt at doing it with the client filter, and I will be glad to reopen the question. – Paul Samsotha Jun 22 '16 at 14:09
  • OHH a small overlook, lemme do some testing super quick. Thanks, I got kinda salty XD my bad. – Kevin Thorne Jun 22 '16 at 14:12
  • 1
    I'll re-open the question anyway. I don't think the solution is obvious enough . You basically will need to rebuild the request uri, using the UriBuidler. Do something like `UriBuilder.fromUri(contexr.getRequestUri()).queryParam(...).build()`. And then set the request URI using the new URI – Paul Samsotha Jun 22 '16 at 14:16

1 Answers1

9

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);
Kevin Thorne
  • 435
  • 5
  • 19