开发者社区 问答 正文

API 网关的path参数如何编码?

API 网关的path参数如何编码?

展开
收起
保持可爱mmm 2020-03-26 21:41:10 827 分享 版权
1 条回答
写回答
取消 提交回答
  • 当API的path中有参数,而且参数含特殊字符。需要先将参数urlencode后加入path中。不然签名会有问题。此时后端收到的也是urlEncode后的值,如果要获取原值,需要自己在后端进行urlDecode.

        @Test
        public void testPath() throws Exception{
            //请求path
            String host="你的域名";
            //请求path,先将参数处理后再放入path中。        
            String type="中文 123";
            String pathParam= URLEncoder.encode(type,"UTF-8");
            String path = "/"+pathParam;
            Map<String, String> headers = new HashMap<String, String>();
            //(必填)根据期望的Response内容类型设置
            headers.put(HttpHeader.HTTP_HEADER_ACCEPT, "application/json");
            CUSTOM_HEADERS_TO_SIGN_PREFIX.clear();
            Request request = new Request(Method.GET, host, path, AppKey, AppSecret, Constants.DEFAULT_TIMEOUT);
            request.setHeaders(headers);
            request.setSignHeaderPrefixList(CUSTOM_HEADERS_TO_SIGN_PREFIX);
            //调用服务端
            Response response = Client.execute(request);
            System.out.println(JSON.toJSONString(response));
        }
    
    2020-03-26 21:42:50
    赞同 展开评论