0

Precisely said I want to perform below curl action which returns json with java:

curl -H 'Client-ID: ahh_got_ya' -X GET 'https://api.twitch.tv/helix/streams'

This works just fine in linux shell.

below is my script trying to do above curl using java json:

{String urly = "https://api.twitch.tv/helix/streams";
    URL obj = new URL(urly);
    HttpURLConnection con = (HttpURLConnection) obj.openConnection();

    con.setRequestMethod("GET");
    con.setRequestProperty("Content-Type","application/json");
    con.setRequestProperty("Client-ID","Ahh_got_ya");


    con.setDoOutput(true);
    DataOutputStream wr = new DataOutputStream(con.getOutputStream());
    wr.writeBytes("");
    wr.flush();
    wr.close();

    int responseCode = con.getResponseCode();
    System.out.println("Response Code : " + responseCode);

    BufferedReader iny = new BufferedReader(
    new InputStreamReader(con.getInputStream()));
      String output;
      StringBuffer jsonres = new StringBuffer();

      while ((output = iny.readLine()) != null) {
          jsonres.append(output);
      }
    iny.close();

    //printing result from response
    System.out.println(response.toString());
}

I am getting: java.io.FileNotFoundException: https://api.twitch.tv/helix/streams Response Code : 404

All replies are much appreciated.

5
  • 1
    What is "java json"? Commented Dec 1, 2018 at 22:38
  • @GeorgeJempty performing curl using json in java Commented Dec 1, 2018 at 22:41
  • Where is the "curl" in "java json" Commented Dec 1, 2018 at 22:45
  • @GeorgeJempty curl is used in linux shell, u can use it to specify packet header, content, etc, etc and do http GET and POST stuff.. so the question is: i want to craft similar packet as crafted with above curl command. Commented Dec 1, 2018 at 22:49
  • I know all about it. The way you've worded things though makes no sense at all. In particular "using json" which has nothing to do with using curl from Java. In retrospect I should have voted to close on the grounds that this is "too broad", not "unclear" Commented Dec 1, 2018 at 23:49

2 Answers 2

1

Almost there! You are doing a GET call and do not need to make the connection writeable -- since you are not going to post. You need to remove that section there. Also - to get exactly what your curl call is doing, remove the Content-Type - since it is not used in the curl call. So your code adjusted should be:

{
    String urly = "https://api.twitch.tv/helix/streams";
    URL obj = new URL(urly);
    HttpURLConnection con = (HttpURLConnection) obj.openConnection();

    //only 2 headers from cURL call
    con.setRequestMethod("GET");
    con.setRequestProperty("Client-ID","Ahh_got_ya");

    int responseCode = con.getResponseCode();
    System.out.println("Response Code : " + responseCode);

    BufferedReader iny = new BufferedReader(
    new InputStreamReader(con.getInputStream()));
      String output;
      StringBuffer jsonres = new StringBuffer();

      while ((output = iny.readLine()) != null) {
          jsonres.append(output);
      }
    iny.close();

    //printing result from response
    System.out.println(response.toString());
}

The reason for the 404 is if your request does not match what the service endpoint is expecting. Sending a POST request or other types of non-expect stuff will result is a request that does not match. Remove the extra output stuff and give it a go!

Sign up to request clarification or add additional context in comments.

Comments

0

The way you state your question is a bit weird. But I assume you want to let a Java program make a cURL call of a JSON file. Now your linux terminal talks BASH not Java. So here is step 1.

You have to use a library.
Options are java.net.URL and/or java.net.URLConnection.

So #include one or either of those.

URL url = new URL("https://api.twitch.tv/helix/streams");

try (BufferedReader reader = new BufferedReader(new 
InputStreamReader(url.openStream(), "UTF-8"))) {
    for (String line; (line = reader.readLine()) != null;) {
        System.out.println(line);
    }
}

https://docs.oracle.com/javase/tutorial/networking/urls/readingWriting.html

Another thing you could mean is you want Java to generate JSON and access cURL trough Bash which isn't something I would advise anyone to do. If you feel like you have to it would be something like this.

public class ExecuteShellCommand {

public String executeCommand(String command) {

With the string set to cURL

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.