0

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!

masiboo
  • 4,537
  • 9
  • 75
  • 136
  • 2
    Well, your curl sets a header named `X-Access-Token`, and having the value `eyJ6aXAiOiJERUYiLCJlbmMiOiJBMjU2...`. Whereas your Java code sets a header names `Authorization`, with the value `Token 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. – JB Nizet Mar 18 '19 at 15:28
  • Hi, that was my question. I don't know how to set token by Java code. I google it. But I didn't find anything so far. Pls, let me know how to set the token in Java. Btw, the token here is just an example. It is very long in the actual code. – masiboo Mar 19 '19 at 06:48
  • After reading ur reply carefully I undersand more. Yes, I tried the code as on.setRequestProperty("X-Access-Token:", token); It didn't work. Exception message: Illegal character(s) in message header field: X-Access-Token: – masiboo Mar 19 '19 at 07:07
  • `:` is the separator. The header is named `X-Access-Token`. This should be documented by the API you're trying to use. – JB Nizet Mar 19 '19 at 07:51
  • Thanks after removing ":" it worked perfectly. If you could give it as an answer I could mark it as the correct answer. – masiboo Mar 19 '19 at 08:01

0 Answers0