Why is the url parameter encoded?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
1
down vote

favorite












Let's say I have a map object which will be converted into URL parameter.



Map<String, Object> params = new Map<String, Object>'accountId' => '0x10'



https://test.com/sample_vf_page_name?accountId=%220x10%22



I don't get how come it show %22 in the url parameters. How could I get rid of this issue from Salesforce



 Map<String, String> mapObj = new Map<String, String>();
mapObj.put(paramName, paramValue);
for (String key : extraParameters.keySet())
mapObj.put(key, JSON.serialize(extraParameters.get(key)));

PageReference pageReference = new PageReference(redirectUrl);
pageReference.getParameters().putAll(mapObj);
return pageReference.getUrl();









share|improve this question





























    up vote
    1
    down vote

    favorite












    Let's say I have a map object which will be converted into URL parameter.



    Map<String, Object> params = new Map<String, Object>'accountId' => '0x10'



    https://test.com/sample_vf_page_name?accountId=%220x10%22



    I don't get how come it show %22 in the url parameters. How could I get rid of this issue from Salesforce



     Map<String, String> mapObj = new Map<String, String>();
    mapObj.put(paramName, paramValue);
    for (String key : extraParameters.keySet())
    mapObj.put(key, JSON.serialize(extraParameters.get(key)));

    PageReference pageReference = new PageReference(redirectUrl);
    pageReference.getParameters().putAll(mapObj);
    return pageReference.getUrl();









    share|improve this question

























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      Let's say I have a map object which will be converted into URL parameter.



      Map<String, Object> params = new Map<String, Object>'accountId' => '0x10'



      https://test.com/sample_vf_page_name?accountId=%220x10%22



      I don't get how come it show %22 in the url parameters. How could I get rid of this issue from Salesforce



       Map<String, String> mapObj = new Map<String, String>();
      mapObj.put(paramName, paramValue);
      for (String key : extraParameters.keySet())
      mapObj.put(key, JSON.serialize(extraParameters.get(key)));

      PageReference pageReference = new PageReference(redirectUrl);
      pageReference.getParameters().putAll(mapObj);
      return pageReference.getUrl();









      share|improve this question















      Let's say I have a map object which will be converted into URL parameter.



      Map<String, Object> params = new Map<String, Object>'accountId' => '0x10'



      https://test.com/sample_vf_page_name?accountId=%220x10%22



      I don't get how come it show %22 in the url parameters. How could I get rid of this issue from Salesforce



       Map<String, String> mapObj = new Map<String, String>();
      mapObj.put(paramName, paramValue);
      for (String key : extraParameters.keySet())
      mapObj.put(key, JSON.serialize(extraParameters.get(key)));

      PageReference pageReference = new PageReference(redirectUrl);
      pageReference.getParameters().putAll(mapObj);
      return pageReference.getUrl();






      apex pagereference






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 1 hour ago









      Derek F

      17.6k31444




      17.6k31444










      asked 1 hour ago









      Dummy

      847




      847




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          2
          down vote













          Background:



          URLs like "https://salesforce.stackexchange.com" can only contain certain characters.



          The document that outlines this is RFC3986



          From that, the following characters are allowed in a URL




          2.3. Unreserved Characters



          Characters that are allowed in a URI but do not have a reserved
          purpose are called unreserved. These include uppercase and lowercase
          letters, decimal digits, hyphen, period, underscore, and tilde.



           unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"



          Where ALPHA = the latin alphabet (A-Z and a-z) and DIGIT is 0-9.



          There are also characters like the colon :, forward-slash /, question mark ? that have special meanings. These can appear in some places in the url, but not others.



          Everything else needs to be escaped or encoded, and that RFC states that we do this by percent encoding the ascii value of the character we want to use.



          In your case, the ascii value for a double quote is 22, so percent-encoded, that becomes %22.



          So, if you're seeing %22 in your url, then you must be introducing double quotes somewhere



          Why is Salesforce doing this?



          Salesforce is adding the single quotes to your Id string because you're JSON-encoding your parameters.



          Using your example, and expanding on it...



          // Pass #1, the "normal" method that will work as expected
          Map<String, String> mapObj = new Map<String, String>();
          mapObj.put('accountId', '001000000000000AAA');

          PageReference pageReference = new PageReference('/account/');
          pageReference.getParameters().putAll(mapObj);

          system.debug(mapObj);
          system.debug(pageReference.getUrl());

          // Pass #2, JSON encoding
          // Will introduce your double quotes
          mapObj = new Map<String, String>();
          mapObj.put('accountId', JSON.serialize('001000000000000AAA'));

          pageReference = new PageReference('/account/');
          pageReference.getParameters().putAll(mapObj);

          system.debug(mapObj);
          system.debug(pageReference.getUrl());


          The result of running that is:




          13:27:55:007 USER_DEBUG [76]|DEBUG|accountId=001000000000000AAA

          13:27:55:007 USER_DEBUG [77]|DEBUG|/account/?accountId=001000000000000AAA

          Above is the result of the first portion of the code

          Below is the result of the second portion of the code

          13:27:55:007 USER_DEBUG [83]|DEBUG|accountId="001000000000000AAA"

          13:27:55:007 USER_DEBUG [84]|DEBUG|/account/?accountId=%22001000000000000AAA%22




          A JSON string is enclosed in double quotes, which you can see is introduced on debug line 83 (the JSON-encoding example). Salesforce then dutifully percent-encodes those for you so your url is not invalid.



          I seem to remember seeing something in the past couple of years that Unicode URLs or TLDs were being allowed or considered, but that doesn't have much bearing on my answer.






          share|improve this answer




















            Your Answer







            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "459"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f233806%2fwhy-is-the-url-parameter-encoded%23new-answer', 'question_page');

            );

            Post as a guest






























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            2
            down vote













            Background:



            URLs like "https://salesforce.stackexchange.com" can only contain certain characters.



            The document that outlines this is RFC3986



            From that, the following characters are allowed in a URL




            2.3. Unreserved Characters



            Characters that are allowed in a URI but do not have a reserved
            purpose are called unreserved. These include uppercase and lowercase
            letters, decimal digits, hyphen, period, underscore, and tilde.



             unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"



            Where ALPHA = the latin alphabet (A-Z and a-z) and DIGIT is 0-9.



            There are also characters like the colon :, forward-slash /, question mark ? that have special meanings. These can appear in some places in the url, but not others.



            Everything else needs to be escaped or encoded, and that RFC states that we do this by percent encoding the ascii value of the character we want to use.



            In your case, the ascii value for a double quote is 22, so percent-encoded, that becomes %22.



            So, if you're seeing %22 in your url, then you must be introducing double quotes somewhere



            Why is Salesforce doing this?



            Salesforce is adding the single quotes to your Id string because you're JSON-encoding your parameters.



            Using your example, and expanding on it...



            // Pass #1, the "normal" method that will work as expected
            Map<String, String> mapObj = new Map<String, String>();
            mapObj.put('accountId', '001000000000000AAA');

            PageReference pageReference = new PageReference('/account/');
            pageReference.getParameters().putAll(mapObj);

            system.debug(mapObj);
            system.debug(pageReference.getUrl());

            // Pass #2, JSON encoding
            // Will introduce your double quotes
            mapObj = new Map<String, String>();
            mapObj.put('accountId', JSON.serialize('001000000000000AAA'));

            pageReference = new PageReference('/account/');
            pageReference.getParameters().putAll(mapObj);

            system.debug(mapObj);
            system.debug(pageReference.getUrl());


            The result of running that is:




            13:27:55:007 USER_DEBUG [76]|DEBUG|accountId=001000000000000AAA

            13:27:55:007 USER_DEBUG [77]|DEBUG|/account/?accountId=001000000000000AAA

            Above is the result of the first portion of the code

            Below is the result of the second portion of the code

            13:27:55:007 USER_DEBUG [83]|DEBUG|accountId="001000000000000AAA"

            13:27:55:007 USER_DEBUG [84]|DEBUG|/account/?accountId=%22001000000000000AAA%22




            A JSON string is enclosed in double quotes, which you can see is introduced on debug line 83 (the JSON-encoding example). Salesforce then dutifully percent-encodes those for you so your url is not invalid.



            I seem to remember seeing something in the past couple of years that Unicode URLs or TLDs were being allowed or considered, but that doesn't have much bearing on my answer.






            share|improve this answer
























              up vote
              2
              down vote













              Background:



              URLs like "https://salesforce.stackexchange.com" can only contain certain characters.



              The document that outlines this is RFC3986



              From that, the following characters are allowed in a URL




              2.3. Unreserved Characters



              Characters that are allowed in a URI but do not have a reserved
              purpose are called unreserved. These include uppercase and lowercase
              letters, decimal digits, hyphen, period, underscore, and tilde.



               unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"



              Where ALPHA = the latin alphabet (A-Z and a-z) and DIGIT is 0-9.



              There are also characters like the colon :, forward-slash /, question mark ? that have special meanings. These can appear in some places in the url, but not others.



              Everything else needs to be escaped or encoded, and that RFC states that we do this by percent encoding the ascii value of the character we want to use.



              In your case, the ascii value for a double quote is 22, so percent-encoded, that becomes %22.



              So, if you're seeing %22 in your url, then you must be introducing double quotes somewhere



              Why is Salesforce doing this?



              Salesforce is adding the single quotes to your Id string because you're JSON-encoding your parameters.



              Using your example, and expanding on it...



              // Pass #1, the "normal" method that will work as expected
              Map<String, String> mapObj = new Map<String, String>();
              mapObj.put('accountId', '001000000000000AAA');

              PageReference pageReference = new PageReference('/account/');
              pageReference.getParameters().putAll(mapObj);

              system.debug(mapObj);
              system.debug(pageReference.getUrl());

              // Pass #2, JSON encoding
              // Will introduce your double quotes
              mapObj = new Map<String, String>();
              mapObj.put('accountId', JSON.serialize('001000000000000AAA'));

              pageReference = new PageReference('/account/');
              pageReference.getParameters().putAll(mapObj);

              system.debug(mapObj);
              system.debug(pageReference.getUrl());


              The result of running that is:




              13:27:55:007 USER_DEBUG [76]|DEBUG|accountId=001000000000000AAA

              13:27:55:007 USER_DEBUG [77]|DEBUG|/account/?accountId=001000000000000AAA

              Above is the result of the first portion of the code

              Below is the result of the second portion of the code

              13:27:55:007 USER_DEBUG [83]|DEBUG|accountId="001000000000000AAA"

              13:27:55:007 USER_DEBUG [84]|DEBUG|/account/?accountId=%22001000000000000AAA%22




              A JSON string is enclosed in double quotes, which you can see is introduced on debug line 83 (the JSON-encoding example). Salesforce then dutifully percent-encodes those for you so your url is not invalid.



              I seem to remember seeing something in the past couple of years that Unicode URLs or TLDs were being allowed or considered, but that doesn't have much bearing on my answer.






              share|improve this answer






















                up vote
                2
                down vote










                up vote
                2
                down vote









                Background:



                URLs like "https://salesforce.stackexchange.com" can only contain certain characters.



                The document that outlines this is RFC3986



                From that, the following characters are allowed in a URL




                2.3. Unreserved Characters



                Characters that are allowed in a URI but do not have a reserved
                purpose are called unreserved. These include uppercase and lowercase
                letters, decimal digits, hyphen, period, underscore, and tilde.



                 unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"



                Where ALPHA = the latin alphabet (A-Z and a-z) and DIGIT is 0-9.



                There are also characters like the colon :, forward-slash /, question mark ? that have special meanings. These can appear in some places in the url, but not others.



                Everything else needs to be escaped or encoded, and that RFC states that we do this by percent encoding the ascii value of the character we want to use.



                In your case, the ascii value for a double quote is 22, so percent-encoded, that becomes %22.



                So, if you're seeing %22 in your url, then you must be introducing double quotes somewhere



                Why is Salesforce doing this?



                Salesforce is adding the single quotes to your Id string because you're JSON-encoding your parameters.



                Using your example, and expanding on it...



                // Pass #1, the "normal" method that will work as expected
                Map<String, String> mapObj = new Map<String, String>();
                mapObj.put('accountId', '001000000000000AAA');

                PageReference pageReference = new PageReference('/account/');
                pageReference.getParameters().putAll(mapObj);

                system.debug(mapObj);
                system.debug(pageReference.getUrl());

                // Pass #2, JSON encoding
                // Will introduce your double quotes
                mapObj = new Map<String, String>();
                mapObj.put('accountId', JSON.serialize('001000000000000AAA'));

                pageReference = new PageReference('/account/');
                pageReference.getParameters().putAll(mapObj);

                system.debug(mapObj);
                system.debug(pageReference.getUrl());


                The result of running that is:




                13:27:55:007 USER_DEBUG [76]|DEBUG|accountId=001000000000000AAA

                13:27:55:007 USER_DEBUG [77]|DEBUG|/account/?accountId=001000000000000AAA

                Above is the result of the first portion of the code

                Below is the result of the second portion of the code

                13:27:55:007 USER_DEBUG [83]|DEBUG|accountId="001000000000000AAA"

                13:27:55:007 USER_DEBUG [84]|DEBUG|/account/?accountId=%22001000000000000AAA%22




                A JSON string is enclosed in double quotes, which you can see is introduced on debug line 83 (the JSON-encoding example). Salesforce then dutifully percent-encodes those for you so your url is not invalid.



                I seem to remember seeing something in the past couple of years that Unicode URLs or TLDs were being allowed or considered, but that doesn't have much bearing on my answer.






                share|improve this answer












                Background:



                URLs like "https://salesforce.stackexchange.com" can only contain certain characters.



                The document that outlines this is RFC3986



                From that, the following characters are allowed in a URL




                2.3. Unreserved Characters



                Characters that are allowed in a URI but do not have a reserved
                purpose are called unreserved. These include uppercase and lowercase
                letters, decimal digits, hyphen, period, underscore, and tilde.



                 unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"



                Where ALPHA = the latin alphabet (A-Z and a-z) and DIGIT is 0-9.



                There are also characters like the colon :, forward-slash /, question mark ? that have special meanings. These can appear in some places in the url, but not others.



                Everything else needs to be escaped or encoded, and that RFC states that we do this by percent encoding the ascii value of the character we want to use.



                In your case, the ascii value for a double quote is 22, so percent-encoded, that becomes %22.



                So, if you're seeing %22 in your url, then you must be introducing double quotes somewhere



                Why is Salesforce doing this?



                Salesforce is adding the single quotes to your Id string because you're JSON-encoding your parameters.



                Using your example, and expanding on it...



                // Pass #1, the "normal" method that will work as expected
                Map<String, String> mapObj = new Map<String, String>();
                mapObj.put('accountId', '001000000000000AAA');

                PageReference pageReference = new PageReference('/account/');
                pageReference.getParameters().putAll(mapObj);

                system.debug(mapObj);
                system.debug(pageReference.getUrl());

                // Pass #2, JSON encoding
                // Will introduce your double quotes
                mapObj = new Map<String, String>();
                mapObj.put('accountId', JSON.serialize('001000000000000AAA'));

                pageReference = new PageReference('/account/');
                pageReference.getParameters().putAll(mapObj);

                system.debug(mapObj);
                system.debug(pageReference.getUrl());


                The result of running that is:




                13:27:55:007 USER_DEBUG [76]|DEBUG|accountId=001000000000000AAA

                13:27:55:007 USER_DEBUG [77]|DEBUG|/account/?accountId=001000000000000AAA

                Above is the result of the first portion of the code

                Below is the result of the second portion of the code

                13:27:55:007 USER_DEBUG [83]|DEBUG|accountId="001000000000000AAA"

                13:27:55:007 USER_DEBUG [84]|DEBUG|/account/?accountId=%22001000000000000AAA%22




                A JSON string is enclosed in double quotes, which you can see is introduced on debug line 83 (the JSON-encoding example). Salesforce then dutifully percent-encodes those for you so your url is not invalid.



                I seem to remember seeing something in the past couple of years that Unicode URLs or TLDs were being allowed or considered, but that doesn't have much bearing on my answer.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 39 mins ago









                Derek F

                17.6k31444




                17.6k31444



























                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f233806%2fwhy-is-the-url-parameter-encoded%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Comments

                    Popular posts from this blog

                    Long meetings (6-7 hours a day): Being “babysat” by supervisor

                    Is the Concept of Multiple Fantasy Races Scientifically Flawed? [closed]

                    Confectionery