Encoding issue with curly brackets and space in UriBuilder












1















I am using the com.sun.jersey.api.uri.UriBuilderImpl implementation of UriBuilder to encode a URL (version 1.19). The issue arises when the query parameter of my URL includes both curly brackets and a space.



Example:



UriBuilder uriBuilder = fromUri("www.something.com")
.queryParam("q", "{some thing}");

return uriBuilder.build().toString();


This fails with:



javax.ws.rs.core.UriBuilderException: java.net.URISyntaxException: Illegal character in query at index 27: www.something.com?q=%7Bsome thing%7D


Which is interesting, as if I take the curly brackets away, I get the expected encoding: www.something.com?q=some+thing...



org.apache.http.client.utils.URIBuilder encodes that as I would expect, which is: www.something.com?q=%7Bsome+thing%7D



I've tried doing this:



...
.queryParam("q", UriComponent.encode(searchQuery, UriComponent.Type.QUERY_PARAM)).
.build();


But then the space character also gets encoded to www.something.com?q=%7Bsome%2Bthing%7D.



What do I have to do to get www.something.com?q=%7Bsome+thing%7D?










share|improve this question

























  • Where is fromUri from?

    – user10639668
    Nov 22 '18 at 17:18











  • It's from jsr311-api-1.1.1: javax.ws.rs.core.UriBuilder

    – pavlos163
    Nov 22 '18 at 17:21













  • build returns a URI instance, not UriBuilder

    – user10639668
    Nov 22 '18 at 17:24











  • sorry, that was me adding build after I copy-pasted the code here. I'll edit.

    – pavlos163
    Nov 22 '18 at 17:25











  • See my updated answer.

    – user10639668
    Nov 22 '18 at 18:39
















1















I am using the com.sun.jersey.api.uri.UriBuilderImpl implementation of UriBuilder to encode a URL (version 1.19). The issue arises when the query parameter of my URL includes both curly brackets and a space.



Example:



UriBuilder uriBuilder = fromUri("www.something.com")
.queryParam("q", "{some thing}");

return uriBuilder.build().toString();


This fails with:



javax.ws.rs.core.UriBuilderException: java.net.URISyntaxException: Illegal character in query at index 27: www.something.com?q=%7Bsome thing%7D


Which is interesting, as if I take the curly brackets away, I get the expected encoding: www.something.com?q=some+thing...



org.apache.http.client.utils.URIBuilder encodes that as I would expect, which is: www.something.com?q=%7Bsome+thing%7D



I've tried doing this:



...
.queryParam("q", UriComponent.encode(searchQuery, UriComponent.Type.QUERY_PARAM)).
.build();


But then the space character also gets encoded to www.something.com?q=%7Bsome%2Bthing%7D.



What do I have to do to get www.something.com?q=%7Bsome+thing%7D?










share|improve this question

























  • Where is fromUri from?

    – user10639668
    Nov 22 '18 at 17:18











  • It's from jsr311-api-1.1.1: javax.ws.rs.core.UriBuilder

    – pavlos163
    Nov 22 '18 at 17:21













  • build returns a URI instance, not UriBuilder

    – user10639668
    Nov 22 '18 at 17:24











  • sorry, that was me adding build after I copy-pasted the code here. I'll edit.

    – pavlos163
    Nov 22 '18 at 17:25











  • See my updated answer.

    – user10639668
    Nov 22 '18 at 18:39














1












1








1








I am using the com.sun.jersey.api.uri.UriBuilderImpl implementation of UriBuilder to encode a URL (version 1.19). The issue arises when the query parameter of my URL includes both curly brackets and a space.



Example:



UriBuilder uriBuilder = fromUri("www.something.com")
.queryParam("q", "{some thing}");

return uriBuilder.build().toString();


This fails with:



javax.ws.rs.core.UriBuilderException: java.net.URISyntaxException: Illegal character in query at index 27: www.something.com?q=%7Bsome thing%7D


Which is interesting, as if I take the curly brackets away, I get the expected encoding: www.something.com?q=some+thing...



org.apache.http.client.utils.URIBuilder encodes that as I would expect, which is: www.something.com?q=%7Bsome+thing%7D



I've tried doing this:



...
.queryParam("q", UriComponent.encode(searchQuery, UriComponent.Type.QUERY_PARAM)).
.build();


But then the space character also gets encoded to www.something.com?q=%7Bsome%2Bthing%7D.



What do I have to do to get www.something.com?q=%7Bsome+thing%7D?










share|improve this question
















I am using the com.sun.jersey.api.uri.UriBuilderImpl implementation of UriBuilder to encode a URL (version 1.19). The issue arises when the query parameter of my URL includes both curly brackets and a space.



Example:



UriBuilder uriBuilder = fromUri("www.something.com")
.queryParam("q", "{some thing}");

return uriBuilder.build().toString();


This fails with:



javax.ws.rs.core.UriBuilderException: java.net.URISyntaxException: Illegal character in query at index 27: www.something.com?q=%7Bsome thing%7D


Which is interesting, as if I take the curly brackets away, I get the expected encoding: www.something.com?q=some+thing...



org.apache.http.client.utils.URIBuilder encodes that as I would expect, which is: www.something.com?q=%7Bsome+thing%7D



I've tried doing this:



...
.queryParam("q", UriComponent.encode(searchQuery, UriComponent.Type.QUERY_PARAM)).
.build();


But then the space character also gets encoded to www.something.com?q=%7Bsome%2Bthing%7D.



What do I have to do to get www.something.com?q=%7Bsome+thing%7D?







java encoding jersey uribuilder






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 17:25







pavlos163

















asked Nov 22 '18 at 16:59









pavlos163pavlos163

499844




499844













  • Where is fromUri from?

    – user10639668
    Nov 22 '18 at 17:18











  • It's from jsr311-api-1.1.1: javax.ws.rs.core.UriBuilder

    – pavlos163
    Nov 22 '18 at 17:21













  • build returns a URI instance, not UriBuilder

    – user10639668
    Nov 22 '18 at 17:24











  • sorry, that was me adding build after I copy-pasted the code here. I'll edit.

    – pavlos163
    Nov 22 '18 at 17:25











  • See my updated answer.

    – user10639668
    Nov 22 '18 at 18:39



















  • Where is fromUri from?

    – user10639668
    Nov 22 '18 at 17:18











  • It's from jsr311-api-1.1.1: javax.ws.rs.core.UriBuilder

    – pavlos163
    Nov 22 '18 at 17:21













  • build returns a URI instance, not UriBuilder

    – user10639668
    Nov 22 '18 at 17:24











  • sorry, that was me adding build after I copy-pasted the code here. I'll edit.

    – pavlos163
    Nov 22 '18 at 17:25











  • See my updated answer.

    – user10639668
    Nov 22 '18 at 18:39

















Where is fromUri from?

– user10639668
Nov 22 '18 at 17:18





Where is fromUri from?

– user10639668
Nov 22 '18 at 17:18













It's from jsr311-api-1.1.1: javax.ws.rs.core.UriBuilder

– pavlos163
Nov 22 '18 at 17:21







It's from jsr311-api-1.1.1: javax.ws.rs.core.UriBuilder

– pavlos163
Nov 22 '18 at 17:21















build returns a URI instance, not UriBuilder

– user10639668
Nov 22 '18 at 17:24





build returns a URI instance, not UriBuilder

– user10639668
Nov 22 '18 at 17:24













sorry, that was me adding build after I copy-pasted the code here. I'll edit.

– pavlos163
Nov 22 '18 at 17:25





sorry, that was me adding build after I copy-pasted the code here. I'll edit.

– pavlos163
Nov 22 '18 at 17:25













See my updated answer.

– user10639668
Nov 22 '18 at 18:39





See my updated answer.

– user10639668
Nov 22 '18 at 18:39












2 Answers
2






active

oldest

votes


















1














I'm using jersey-client v1.9.1 and your code run with no error.



Here is my maven dependency:



<!-- https://mvnrepository.com/artifact/com.sun.jersey/jersey-client -->
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
<version>1.9.1</version>
</dependency>


And the java code:



import java.net.URI;

import javax.ws.rs.core.UriBuilder;

public class Main {
public static void main(String args) throws Exception {
try {
URI url = UriBuilder.fromUri("www.something.com")
.queryParam("q", "{some thing}")
.build();
System.out.println(url);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}


The output: www.something.com?q=%7Bsome+thing%7D



UPDATE



The curly braces are the reason for failing with v1.19 (see the documentation)



Here is a working version for v1.19:



        URI url = UriBuilder.fromPath("www.something.com")
.queryParam("q", "{value}")
.build("{some thing}", "value");
System.out.println(url);





share|improve this answer


























  • You are right! It works in 1.9.1 but not in the version I'm using...

    – pavlos163
    Nov 22 '18 at 18:23













  • @pavlos163 Yes, the space seems to be the problem.

    – user10639668
    Nov 22 '18 at 18:26











  • That works. It's a bit weird, and I wonder if they've fixed in in newer version of Jersey... But yeah for now that's what I'll do, thanks.

    – pavlos163
    Nov 23 '18 at 9:43



















0














You can try this:
You need two library
jersey-core-1.19.2 and javax.ws.rs and jersey-client-1.19.2



public class ClassTest {
public static void main(String args) throws UnsupportedEncodingException {
String queryString = URLEncoder.encode("{some thing}", "UTF-8");
System.out.println(queryString);
URI uri = UriBuilder.fromUri("www.something.com")
.queryParam("q", queryString)
.build();
String url = uri.toString().replace("%2B", "+");
System.out.println(url);
}
}





share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    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',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53435493%2fencoding-issue-with-curly-brackets-and-space-in-uribuilder%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    I'm using jersey-client v1.9.1 and your code run with no error.



    Here is my maven dependency:



    <!-- https://mvnrepository.com/artifact/com.sun.jersey/jersey-client -->
    <dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-client</artifactId>
    <version>1.9.1</version>
    </dependency>


    And the java code:



    import java.net.URI;

    import javax.ws.rs.core.UriBuilder;

    public class Main {
    public static void main(String args) throws Exception {
    try {
    URI url = UriBuilder.fromUri("www.something.com")
    .queryParam("q", "{some thing}")
    .build();
    System.out.println(url);
    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }

    }


    The output: www.something.com?q=%7Bsome+thing%7D



    UPDATE



    The curly braces are the reason for failing with v1.19 (see the documentation)



    Here is a working version for v1.19:



            URI url = UriBuilder.fromPath("www.something.com")
    .queryParam("q", "{value}")
    .build("{some thing}", "value");
    System.out.println(url);





    share|improve this answer


























    • You are right! It works in 1.9.1 but not in the version I'm using...

      – pavlos163
      Nov 22 '18 at 18:23













    • @pavlos163 Yes, the space seems to be the problem.

      – user10639668
      Nov 22 '18 at 18:26











    • That works. It's a bit weird, and I wonder if they've fixed in in newer version of Jersey... But yeah for now that's what I'll do, thanks.

      – pavlos163
      Nov 23 '18 at 9:43
















    1














    I'm using jersey-client v1.9.1 and your code run with no error.



    Here is my maven dependency:



    <!-- https://mvnrepository.com/artifact/com.sun.jersey/jersey-client -->
    <dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-client</artifactId>
    <version>1.9.1</version>
    </dependency>


    And the java code:



    import java.net.URI;

    import javax.ws.rs.core.UriBuilder;

    public class Main {
    public static void main(String args) throws Exception {
    try {
    URI url = UriBuilder.fromUri("www.something.com")
    .queryParam("q", "{some thing}")
    .build();
    System.out.println(url);
    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }

    }


    The output: www.something.com?q=%7Bsome+thing%7D



    UPDATE



    The curly braces are the reason for failing with v1.19 (see the documentation)



    Here is a working version for v1.19:



            URI url = UriBuilder.fromPath("www.something.com")
    .queryParam("q", "{value}")
    .build("{some thing}", "value");
    System.out.println(url);





    share|improve this answer


























    • You are right! It works in 1.9.1 but not in the version I'm using...

      – pavlos163
      Nov 22 '18 at 18:23













    • @pavlos163 Yes, the space seems to be the problem.

      – user10639668
      Nov 22 '18 at 18:26











    • That works. It's a bit weird, and I wonder if they've fixed in in newer version of Jersey... But yeah for now that's what I'll do, thanks.

      – pavlos163
      Nov 23 '18 at 9:43














    1












    1








    1







    I'm using jersey-client v1.9.1 and your code run with no error.



    Here is my maven dependency:



    <!-- https://mvnrepository.com/artifact/com.sun.jersey/jersey-client -->
    <dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-client</artifactId>
    <version>1.9.1</version>
    </dependency>


    And the java code:



    import java.net.URI;

    import javax.ws.rs.core.UriBuilder;

    public class Main {
    public static void main(String args) throws Exception {
    try {
    URI url = UriBuilder.fromUri("www.something.com")
    .queryParam("q", "{some thing}")
    .build();
    System.out.println(url);
    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }

    }


    The output: www.something.com?q=%7Bsome+thing%7D



    UPDATE



    The curly braces are the reason for failing with v1.19 (see the documentation)



    Here is a working version for v1.19:



            URI url = UriBuilder.fromPath("www.something.com")
    .queryParam("q", "{value}")
    .build("{some thing}", "value");
    System.out.println(url);





    share|improve this answer















    I'm using jersey-client v1.9.1 and your code run with no error.



    Here is my maven dependency:



    <!-- https://mvnrepository.com/artifact/com.sun.jersey/jersey-client -->
    <dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-client</artifactId>
    <version>1.9.1</version>
    </dependency>


    And the java code:



    import java.net.URI;

    import javax.ws.rs.core.UriBuilder;

    public class Main {
    public static void main(String args) throws Exception {
    try {
    URI url = UriBuilder.fromUri("www.something.com")
    .queryParam("q", "{some thing}")
    .build();
    System.out.println(url);
    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }

    }


    The output: www.something.com?q=%7Bsome+thing%7D



    UPDATE



    The curly braces are the reason for failing with v1.19 (see the documentation)



    Here is a working version for v1.19:



            URI url = UriBuilder.fromPath("www.something.com")
    .queryParam("q", "{value}")
    .build("{some thing}", "value");
    System.out.println(url);






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 22 '18 at 18:39

























    answered Nov 22 '18 at 17:33







    user10639668




















    • You are right! It works in 1.9.1 but not in the version I'm using...

      – pavlos163
      Nov 22 '18 at 18:23













    • @pavlos163 Yes, the space seems to be the problem.

      – user10639668
      Nov 22 '18 at 18:26











    • That works. It's a bit weird, and I wonder if they've fixed in in newer version of Jersey... But yeah for now that's what I'll do, thanks.

      – pavlos163
      Nov 23 '18 at 9:43



















    • You are right! It works in 1.9.1 but not in the version I'm using...

      – pavlos163
      Nov 22 '18 at 18:23













    • @pavlos163 Yes, the space seems to be the problem.

      – user10639668
      Nov 22 '18 at 18:26











    • That works. It's a bit weird, and I wonder if they've fixed in in newer version of Jersey... But yeah for now that's what I'll do, thanks.

      – pavlos163
      Nov 23 '18 at 9:43

















    You are right! It works in 1.9.1 but not in the version I'm using...

    – pavlos163
    Nov 22 '18 at 18:23







    You are right! It works in 1.9.1 but not in the version I'm using...

    – pavlos163
    Nov 22 '18 at 18:23















    @pavlos163 Yes, the space seems to be the problem.

    – user10639668
    Nov 22 '18 at 18:26





    @pavlos163 Yes, the space seems to be the problem.

    – user10639668
    Nov 22 '18 at 18:26













    That works. It's a bit weird, and I wonder if they've fixed in in newer version of Jersey... But yeah for now that's what I'll do, thanks.

    – pavlos163
    Nov 23 '18 at 9:43





    That works. It's a bit weird, and I wonder if they've fixed in in newer version of Jersey... But yeah for now that's what I'll do, thanks.

    – pavlos163
    Nov 23 '18 at 9:43













    0














    You can try this:
    You need two library
    jersey-core-1.19.2 and javax.ws.rs and jersey-client-1.19.2



    public class ClassTest {
    public static void main(String args) throws UnsupportedEncodingException {
    String queryString = URLEncoder.encode("{some thing}", "UTF-8");
    System.out.println(queryString);
    URI uri = UriBuilder.fromUri("www.something.com")
    .queryParam("q", queryString)
    .build();
    String url = uri.toString().replace("%2B", "+");
    System.out.println(url);
    }
    }





    share|improve this answer






























      0














      You can try this:
      You need two library
      jersey-core-1.19.2 and javax.ws.rs and jersey-client-1.19.2



      public class ClassTest {
      public static void main(String args) throws UnsupportedEncodingException {
      String queryString = URLEncoder.encode("{some thing}", "UTF-8");
      System.out.println(queryString);
      URI uri = UriBuilder.fromUri("www.something.com")
      .queryParam("q", queryString)
      .build();
      String url = uri.toString().replace("%2B", "+");
      System.out.println(url);
      }
      }





      share|improve this answer




























        0












        0








        0







        You can try this:
        You need two library
        jersey-core-1.19.2 and javax.ws.rs and jersey-client-1.19.2



        public class ClassTest {
        public static void main(String args) throws UnsupportedEncodingException {
        String queryString = URLEncoder.encode("{some thing}", "UTF-8");
        System.out.println(queryString);
        URI uri = UriBuilder.fromUri("www.something.com")
        .queryParam("q", queryString)
        .build();
        String url = uri.toString().replace("%2B", "+");
        System.out.println(url);
        }
        }





        share|improve this answer















        You can try this:
        You need two library
        jersey-core-1.19.2 and javax.ws.rs and jersey-client-1.19.2



        public class ClassTest {
        public static void main(String args) throws UnsupportedEncodingException {
        String queryString = URLEncoder.encode("{some thing}", "UTF-8");
        System.out.println(queryString);
        URI uri = UriBuilder.fromUri("www.something.com")
        .queryParam("q", queryString)
        .build();
        String url = uri.toString().replace("%2B", "+");
        System.out.println(url);
        }
        }






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 22 '18 at 18:21

























        answered Nov 22 '18 at 17:53









        flopcoderflopcoder

        735512




        735512






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53435493%2fencoding-issue-with-curly-brackets-and-space-in-uribuilder%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Wiesbaden

            Marschland

            Dieringhausen