简体   繁体   中英

REST API, can't put “%20” or “+” in my URL for spaces. The URL isn't being fetched

I receive an error saying org.springframework.web.client.HttpClientErrorException: 400 null from the console meaning that my URL sent a bad request that the server could not understand.

However, whenever i put a URL with no %20 or + signs for the spaces

Example: http://ksr-ca-qmaltjira.ca.kronos.com:8061/rest/api/2/search?jql=project=SUP&maxResults=2 it works and my data is displayed on my local page.

String username = "confidential";
String password = "confidential";

private static final String jiraBaseURL = "http://ksr-ca-qmaltjira.ca.kronos.com:8061/rest/api/search?jql=project%3D%22Customer%20Support%22%20%26%20type%20%3D%20%22Change%20Request%22%20&maxResults=2";
private RestTemplate restTemplate;
private HttpHeaders httpHeaders;





private HttpHeaders createHeadersWithAuthentication() {
    String plainCreds = username + ":" + password;
    byte[] base64CredsBytes = Base64.getEncoder().encode(plainCreds.getBytes());
    String base64Creds = new String(base64CredsBytes);

    HttpHeaders headers = new HttpHeaders();
    headers.add("Authorization", "Basic " + base64Creds);

    return headers;
}




@RequestMapping("/cr-follow-up")
public @ResponseBody ResponseEntity<String> getData()
{
    restTemplate = new RestTemplate();
    httpHeaders = createHeadersWithAuthentication();
    httpHeaders.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
    HttpEntity<String> entity = new HttpEntity<String>(httpHeaders);
    ResponseEntity<String> response = restTemplate.exchange(jiraBaseURL, HttpMethod.GET, entity, String.class);

    return response;
}

尝试使用URLEncoder.encode()对URL进行编码

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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