2

I have come across similar questions but they don't seem to offer a direct example of doing it. I am trying to use RestTemplate to GET with an encoded url. I have an amazon class helper to generate a signed url but returns the url encoded so when I use it in RestTemplate it is encoded again. The url I am supposed to feed into RestTemplate should look like this;

http://webservices.amazon.com/onca/xml?AWSAccessKeyId=REMOVED&AssociateTag=REMOVED&Keywords=php&Operation=ItemSearch&ResponseGroup=Images,ItemAttributes,Offers&SearchIndex=All&Service=AWSECommerceService&Timestamp=2017-09-05T06:47:25.703Z&Signature=dthAE5BwmK2aZmSoIPRBwsPgCNwIv6JnXoqjC0QyRCQ=

But my amazon helper gives me this;

http://webservices.amazon.com/onca/xml?AWSAccessKeyId=REMOVED&AssociateTag=REMOVED&Keywords=php&Operation=ItemSearch&ResponseGroup=Images%2CItemAttributes%2COffers&SearchIndex=All&Service=AWSECommerceService&Timestamp=2017-09-05T06%3A47%3A25.703Z&Signature=dthAE5BwmK2aZmSoIPRBwsPgCNwIv6JnXoqjC0QyRCQ%3D

Which is again encoded breaking my timestamp. I see I could very easily use string replace but I looking for a better way of doing it.

My code looks like this;

SignedRequestsHelper helper;

    try {
        helper = SignedRequestsHelper.getInstance(this.ENDPOINT, this.ACCESS_KEY_ID, this.SECRET_KEY);
    } catch (Exception e) {
        return "ERROR";
        //e.printStackTrace();
    }

    String requestUrl = null;

    Map<String, String> params = new HashMap<String, String>();
    ...
    requestUrl = helper.sign(params);
    RestTemplate restTemplate = new RestTemplate();
    restTemplate.setErrorHandler(new ResponseErrorHandler());
    ...
    try {
        String response = restTemplate.getForObject(requestUrl, String.class);
    } catch (HttpStatusCodeException exception) {
        return exception.getStatusCode().toString();
    }
0

1 Answer 1

2

Get into the habit of reading the Javadoc:

For each HTTP method there are three variants: two accept a URI template string and URI variables (array or map) while a third accepts a URI. Note that for URI templates it is assumed encoding is necessary, e.g. restTemplate.getForObject("http://example.com/hotel list") becomes "http://example.com/hotel%20list". This also means if the URI template or URI variables are already encoded, double encoding will occur, e.g. http://example.com/hotel%20list becomes http://example.com/hotel%2520list). To avoid that use a URI method variant to provide (or re-use) a previously encoded URI. To prepare such an URI with full control over encoding, consider using UriComponentsBuilder.

Sign up to request clarification or add additional context in comments.

4 Comments

Okay, I see 3 variants for getForObject. And I have seen the UriComponentsBuilder. Supposing I have an encoded url to start with. How do I decode it?
@user5500750 you won't decode it, you will convert it to a URI and use one of the overloaded methods that accept a URI. can the above verbiage be any clearer?
Except I already have an encoded url created by the helper. You can see that in my code. I am already feeding my params into the helper returning a url. I am not looking to create a URI template string and then feed that into the getForObject method. I am new in Java so hopefully I am making sense. I realise what you're saying but putting it into code is the problem.
@user5500750 If you're new to Java, you're the wrong person to write code for a Spring project.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.