I am doing a GET request and this code, which works for all of our existing tests, is giving 400 error for a new endpoint. I can hit the endpoint fine in postman. conn.getErrorStream() doesn't print out anything. The code seems to fail (without throwing exception)right at url.openConnection. If I do conn.getResponseCode() right after that line it returns 400. I thought perhaps it wasn't liking the pipe char I have in my query but encoding it via URLEncoder.encode(resourceContext) as well as URLEncoder.encode(baseUrl + resourceContext) didn't help and ended up throwing MalformedURLException. Here is the endpoint I want to hit (anonymized): https://usqa0003d.usa.company.com:17462/positions/1535000400000/CUR/USA|415|415|CUST|GL53I7TPYBYM|MATCHED
When I attach the debugger to the server I want to hit nothing happens so the request never makes it to the server. I step into openConnection which leads to a rabbit hole full of var5, var6 etc. I switched to HttpsURLConnection but no dice there either. Any other suggestions?
URL url = new URL(baseUrl + resourceContext);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod(requestType);
if(messageLibraryJson) {
conn.setRequestProperty("Content-Type", "application/json;class=true");
conn.setRequestProperty("Accept", "application/json;class=true");
} else {
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("Accept", "application/json");
}
String userpass = username + ":" + password;
String basicAuth = "Basic " + javax.xml.bind.DatatypeConverter.printBase64Binary(userpass.getBytes());
conn.setRequestProperty("Authorization", basicAuth);
if(requestType.equals("POST")) {
OutputStream os = conn.getOutputStream();
os.write(requestJson.getBytes());
os.flush();
}
if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
logger.error("ERROR in REST CLIENT");
logger.error("URL = " + baseUrl + resourceContext);
logger.error("Request = " + requestJson);
throw new RuntimeException("Failed : HTTP error code : "
+ conn.getResponseCode());
}
BufferedReader br = new BufferedReader(new InputStreamReader(
(conn.getInputStream())));
Indeed it was the '|' char causing trouble. If I do the following:
String urlString = (baseUrl + resourceContext).replace("|", "%7C");
URL url = new URL(urlString);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
It works. The following urlString is produced: https://usqa0003d.usa.company.com:17462/positions/1535000400000/CUR/USA%7C415%7C415%7CCUST%7CGL53I7TPYBYM%7CMATCHED
However, when I do:
String enc = baseUrl + URLEncoder.encode(resourceContext);
The '/' is replaced by '%2F' (in addition to the pipe char) and for some reason that causes trouble. For reference, the endpoint is actually defined as such:
@RequestMapping(value = "/{param1}/{param2}/{param3:.*}", method = RequestMethod.GET)
With the class level endpoint (how is that called???) as:
@RequestMapping(value = "/positions",
produces = {"application/vnd.cme.cdo+json", "application/json;class=true", "application/x-protobuf", "application/x-protobuf-text"},
consumes = {"application/vnd.cme.cdo+json", "application/json;class=true", "application/x-protobuf", "application/x-protobuf-text"})
public class PositionController implements IPositionController {
Can anyone comment on why converting '/' to '%2F' causes trouble?
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.