javaspringapache-httpclient-4.xuribuilder

Apache Http UrlBuilder stripping API version from URL


All,

I have my base url defined in a config like so

foobar.baseurl = https://api.foobar.com/v3/reporting/

This is passed as a constructor argument for my Spring bean

<bean id="foobarUriBuilder" class="org.apache.http.client.utils.URIBuilder">
    <constructor-arg value="${foobar.baseurl}"/>
</bean>

This all works fine, however the URLBuilder strips the /v3/reporting part and then when I try to do something like this.

    //Set URI path and query params
    uriBuilder.setPath("/fooReports")
              .setParameter("type_of_foo", FOO_TYPE);

So the request turns into this

https://api.foobar.com/fooReports?type_of_foo=bar

Which gives a HTTP 404 naturally.

So how do I stop URIBuilder stripping a part of my URL so I have the correct URL as below?

https://api.foobar.com/v3/reporting/fooReports?type_of_foo=bar


Solution

  • The /v3/reporting is part of the (original) path. When you set the path to something else, in this case /fooReports, it gets overridden.

    You may try to compose the new path with

    final String originalPath = uriBuilder.getPath();
    uriBuilder.setPath(originalPath + "/fooReports")             
              .setParameter("type_of_foo", FOO_TYPE);