如何在使用borswe URL测试之前对JSON对象进行编码

时间:2012-10-26 11:58:47

标签: json url tapestry

http://localhost/catalog/{"request": "catalog","user_id": "test@gmail.com","purchased": "2"}

这里是我的请求网址。我需要使用在浏览器中输入的示例URL来测试我的服务。但似乎服务器端不接受许多JSON项目。如果我输入平面文本字符串服务器工作正常。我尝试使用http://www.albionresearch.com/misc/urlencode.php对网址进行编码,但仍然存在错误。

可能这是属于挂毯的问题。另外,我想得到一些帮助。

以下请求有效。

 http://localhost/catalog/helloworld

2 个答案:

答案 0 :(得分:1)

tapestry在url中执行自己的参数编码,在客户端没有副本。

请参阅org.apache.tapestry5.internal.services.URLEncoderImpl.encode(String)

'helloworld'按预期工作的原因是没有'特殊字符'所以转义值无论如何都等于'helloworld'。

因此,您需要使用tapestry的URLEncoder通过java对json进行编码,或者编写客户端副本。

即如果我理解你的问题。

编辑我很无聊所以我写了客户端副本:

/**
 * see org.apache.tapestry5.internal.services.URLEncoderImpl.encode(String)
 * correct as at tapestry 5.3.5
 */
function tapestryUrlEncodeParameter(input)
{
    var safe = "abcdefghijklmnopqrstuvwxyz"
            + "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
            + "01234567890-_.:";

    if (input === null)
        return "$N";

    input = input.toString();

    if (input === "")
        return "$B";

    var output = "";

    for (var i = 0; i < input.length; i++)
    {
        var ch = input.charAt(i);

        if (ch === '$')
        {
            output += "$$";
            continue;
        }

        if (safe.indexOf(ch) != -1)
        {
            output += ch;
            continue;
        }

        var chHex = ch.charCodeAt(0).toString(16);
        while (chHex.length < 4)
            chHex = "0" + chHex;
        output += "$" + chHex;
    }

    return output;
}

答案 1 :(得分:0)

你有什么服务器端?无论哪种方式,如果你想这样做,你必须在服务器端解码你编码的json字符串。

更好的解决方案可能是使用某种测试工具。这可能就像网页中的jquery $ .get请求一样简单,或者您可能想要考虑this post

中建议的更通用的HTTP客户端