I tried the following code. Basically, this code is taken from here. There he claims it works for him. But it didn't work for me.
private void doHttp() {
try {
String url = "http://myurl.com/cc/companies/3.88";
String token = "eyJ6aXAiOiJERUYiLCJlbmMiOiJBMjU2R0NNIiwiYWxnIjoiZGlyIn0.....";
URL object = new URL(url);
HttpURLConnection con = (HttpURLConnection) object.openConnection();
con.setDoInput(true);
con.setRequestMethod("GET");
con.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
con.setRequestProperty("Accept", "application/json");
con.setRequestProperty("Authorization:", "Token " + token);
StringBuilder sb = new StringBuilder();
int HttpResult = con.getResponseCode();
if (HttpResult == HttpURLConnection.HTTP_OK) {
BufferedReader br = new BufferedReader(
new InputStreamReader(con.getInputStream(), "utf-8"));
String line = null;
while ((line = br.readLine()) != null) {
sb.append(line + "\n");
}
br.close();
} else {
System.out.println(con.getResponseMessage());
}
} catch (NotFoundException e) {
if (e.getResponse().getStatus() == 404) {
System.out.printf(e.getMessage());
} else {
System.out.printf(e.getMessage());
}
} catch (WebApplicationException e) {
System.out.printf(e.getMessage());
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
System.out.printf(e.getMessage());
}
It always fails with exception message Illegal character(s) in message header field: Authorization:
I try the same request from the terminal as curl -X GET http://myurl.com/cc/companies/3.88 -H "accept: application/json" -H "X-Access-Token:eyJ6aXAiOiJERUYiLCJlbmMiOiJBMjU2..." This works fine.
So how can I make the same from the Java code?
Updated question
After JB Nizet's comment, I managed to make the GET and DELETE request working fine. But now I would like to try POST as well. The question comes again in curl is fine as:-
curl -X POST "http://myurl/app/operator" -H "accept: application/json" -H "X-Access-Token: eyJ6aXAiOiJERUYiLCJlbmMiOiJBMjU2R0NNIiwiYWxnIjoiZGlyIn0..GZ-7sXRhKYHFPKBd.a7tQ2aTYqaKyCp-" **-H "Content-Type: application/json" -d** "{ \"type\": \"OPERATOR\", \"dcpId\": 42968, \"externalId\": null, \"secondaryExternalId\": null, \"name\": \"Connexion\", \"configurationStage\": \"ACTIVE\", \"nodeStatus\": \"SUCCESS\", \"nodeUser\": null, \"serviceRequest\": null, \"additionalInfo\": null, \"serviceId\": null, \"operatorNo\": null, \"operator\": { \"dcpId\": null, \"name\": null, \"operatorNo\": null}"
I don't know how to set the body or -d "{ \"type\": \"OPERATOR\", \"dcpId\": 42968, \"externalId\": null, \"secondaryExternalId\": null, \"name\": \"Connexion\", \"configurationSt.......} by the JAVA code.
I coulnd't find relavent API document for this. Please kindly let me know how can do it in Java. Thanks in advance!
X-Access-Token, and having the valueeyJ6aXAiOiJERUYiLCJlbmMiOiJBMjU2.... Whereas your Java code sets a header namesAuthorization, with the valueToken eyJ6aXAiOiJERUYiLCJlbmMiOiJBMjU2R0NNIiwiYWxnIjoiZGlyIn0...... They clearly don't do the same thing. If it works with the curl request, why do you do something completely different in your Java code? Set the right token, with the right value.:is the separator. The header is namedX-Access-Token. This should be documented by the API you're trying to use.