简体   繁体   English

如何使用URL参数执行GET API请求?

[英]How to do GET API Request with URL params?

Client client = ClientBuilder.newClient();
urlApi="https://localhost:123/demo/api/v1/rows/search?";
WebTarget webTarget = client.target(urlApi);
for (Map.Entry<String, String> entry : queryParams.entrySet()) {
    webTarget.queryParam(entry.getKey(), entry.getValue());
}
webTarget.queryParam("searchConditions",webTarget.queryParam("mobileNo","+9999999999"));

Invocation.Builder builder = webTarget.request();

builder.header("id", "ABC");
String asB64 = Base64.getEncoder().encodeToString("ABC:PWD".getBytes("utf-8"));
logger.debug("Calling  API "+urlApi);
builder.header("Authorization", "Basic "+asB64);
builder.header("Content-type", MediaType.APPLICATION_JSON);     
response = builder.get();
responseData = response.readEntity(String.class);
System.out.println(responseData);

I am trying to do GET request with searchCondition as Key and value as {mobileNo="+919999999999"} , I am unable to get this to work. 我正在尝试使用searchCondition作为键和值为{mobileNo="+919999999999"}来执行GET请求,但无法{mobileNo="+919999999999"}正常工作。

Apart from that, how can I print the Request "Headers" along with "Query params"? 除此之外,如何打印请求“页眉”和“查询参数”? Thank you in advance 先感谢您

I think you need to encode the value inputs, something like this: 我认为您需要对值输入进行编码,如下所示:

webTarget.queryParam("searchCondition", URLEncoder.encode("{mobileNo=\"+919999999999\"}", StandardCharsets.UTF_8.toString()));

UDPATE: Example of the rest client with Spring: UDPATE:Spring其余客户端的示例:

@Test
public void testStack() throws Exception {
    RestTemplate rest = new RestTemplate();
    String fooResourceUrl="http://localhost:8080/usersParam?";
    RestTemplate restTemplate = new RestTemplate();
    String parameter = "{mobileNo=\"+919999999999\"}";

    ResponseEntity<String> response = restTemplate.getForEntity(fooResourceUrl + "parameter=" + URLEncoder.encode(parameter, StandardCharsets.UTF_8.toString() ), String.class);
    assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
}

And this would be the rest service: 这将是其余的服务:

@RequestMapping(method = RequestMethod.GET, value="/usersParam")
    public User getUsersInfo(@RequestParam String parameter) throws UnsupportedEncodingException {
         System.out.println(URLDecoder.decode(parameter, StandardCharsets.UTF_8.toString() ));
        return null;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM