1

I'm trying to extract encoded URL params from a visual force page but I seem to be getting a JSON exception when I do so.

System.currentPageReference().getParameters().put('m', EncodingUtil.urlEncode(JSON.serialize(outgoingMessage), 'UTF-8')); 
System.currentPageReference().getParameters().put('ah', EncodingUtil.urlEncode(expectedMac, 'UTF-8')); 

In this example, the outgoingMessage is of type MyObject (custom object defined as an inner class) to store user data and expectedMac is a string. I'm injecting these params in a test class to mirror the behavior of a request to a visual force page. Once the params are injected, I want to grab them within a controller.

    message            = String.valueOf(ApexPages.currentPage().getParameters().get('m'));
    senderMacString    = String.valueOf(ApexPages.currentPage().getParameters().get('ah')); 

    try{
        incomingMsg  = (MyObject)JSON.deserialize(message, MyObject.class);
    }
    catch(JSONException j){
        System.debug(j.getMessage());
        NotAuthorizedPage();
    } 

To my knowledge, getParameters() will decode the url in utf-8 by default, so I don't need to call use the EncodingUtil.decode method. However, I seem to be getting an error when I try to deserialize that message:

Unexpected character ('%' (code 37)): expected a valid value (number, String, array, object, 'true', 'false' or 'null') at input location [1,2]

Thoughts?

1

In non-test you are correct that the pageReference is decoded.

However in a test method you need to decode it within the test.

I will work up a POC in a few minutes:

Here is a test VF Page to show it works without decoding in NON-test Context

Page

<apex:page id="examplePR" controller="examplePR">
    {!result}
</apex:page>

Controller

public class examplePR {
    public string result {
        get{
            System.debug(ApexPages.currentPage().getParameters().get('example'));
            return ApexPages.currentPage().getParameters().get('example');
        }
    }

}

Hit with the following URL:

https://[instanceURL]/apex/examplePR?example=12%21%40%2634%3F%3D

Results

enter image description here

09:43:09.0 (112324756)|USER_DEBUG|[11]|DEBUG|12!@&34?=

Now here are the test method examples

@IsTest
private class examplePRTest {
    static testMethod void testBehavior() {
        String toEncode = '12!@&34?=';

        ApexPages.currentPage().getParameters().put('example',EncodingUtil.urlEncode(toEncode,'UTF-8'));
        System.debug(ApexPages.currentPage().getParameters().get('example'));

        System.assertEquals(
                toEncode,
                EncodingUtil.urlDecode(
                        ApexPages.currentPage().getParameters().get('example')
                        ,'UTF-8'
                )
        );

        System.debug(
                EncodingUtil.urlDecode(
                        ApexPages.currentPage().getParameters().get('example')
                        ,'UTF-8'
                )
        );

    }
}

09:34:05.1 (2081135)|USER_DEBUG|[15]|DEBUG|12%21%40%2634%3F%3D

09:34:05.1 (2117304)|USER_DEBUG|[16]|DEBUG|12!@&34?=

One of the quirks of testing is that you will need to decode any URL parameters that you get during the test method.

|improve this answer|||||
  • Thank you for the response. For my implementation, I added isTest system method to my controller to properly decode the URL params based on the current context. – Brohan Jul 12 '17 at 21:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.