【问题标题】:How can I set characters like "{" in urls? [duplicate]如何在网址中设置像“{”这样的字符? [复制]
【发布时间】:2016-08-05 02:31:30
【问题描述】:

我需要传递以下网址:

https://api.softlayer.com/rest/v3/SoftLayer_Account/getVirtualGuests?objectFilter={"virtualGuests":{"hostname":{"operation":"hostnameTest"}}}

我尝试了不同的方法,但它不起作用,这是我的代码的一部分:

System.out.println(
                given().
                 when().get("https://api.softlayer.com/rest/v3/SoftLayer_Account/getVirtualGuests?objectFilter={\"virtualGuests\":{\"hostname\":{\"operation\":\"hostnameTest\"}}}").asString());

    }

例外:

java.lang.IllegalArgumentException: Invalid number of path parameters. Expected 1, was 0. Undefined path parameters are: "virtualGuests":{"hostname":{"operation":"hostnameTest".

根据异常,我认为我应该需要使用路径参数,我尝试过但没有成功。

另外,我尝试将 { 替换为字符转义码 %7B

有什么想法吗?提前致谢

【问题讨论】:

    标签: rest-assured


    【解决方案1】:

    我刚试过这个:

    encodeURI('{"virtualGuests":{"hostname":{"operation":"hostnameTest"}}}')
    

    它给了我:

    "%7B%22virtualGuests%22:%7B%22hostname%22:%7B%22operation%22:%22hostnameTest%22%7D%7D%7D"
    

    【讨论】:

      【解决方案2】:

      非常感谢塞巴斯蒂安和罗伯特!

      我没有成功使用 encodeURI,但是我使用了 queryParam 并且它有效

      given().
                              queryParam("objectFilter", "{\"virtualGuests\":{\"hostname\":{\"operation\":\"hostnameTest\"}}}").
                       when().get("/SoftLayer_Account/getVirtualGuests")
                      .then().assertThat().body("id", hasItem(1111111));   
      

      非常感谢!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-01-25
        • 2020-02-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-10-11
        相关资源
        最近更新 更多