0

I am using SpringToolSuite and have been able to code successfully applications that consume data from apis. One different type of API I now need to consume from allows you to access the api with keys that need to be included in the header of the request. Now trying these different requests in a simple REST client on line seems to work but within the code, instead of getting an error response from the API, I get a 500 error. The idea is to be able to make a GET request to get current key details, a POST request when trying to submit queries to the API. I tried to add a header like you would do in .NET (request.addheader) but I found I needed to write to the outstream first. Is my code correct and if not what is the error referencing?

try {


        obj = new URL(url);

        HttpsURLConnection conn = (HttpsURLConnection) obj.openConnection();
        //conn.setRequestMethod("POST");
        conn.setRequestProperty("accept-charset", "UTF-8");
        conn.setRequestProperty("content-type", "application/json");
        conn.setDoOutput(true);     




        OutputStream os = conn.getOutputStream();

        String input = "X-API-Key:" + key;

        os.write(input.getBytes());
        os.flush();

        os.close();

        InputStream urlInputStream;

        BufferedReader inSt = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
        StringBuffer response = new StringBuffer();
        String inputData;

        while((inputData = inSt.readLine()) != null) {
            response.append(inputData);
        }

        inSt.close();
        String fetchResult ="";
        fetchResult = response.toString();

so when debugging, as soon as it hits the line

BufferedReader inSt = new BufferedReader
         (new InputStreamReader(conn.getInputStream(), "UTF-8"));

I get the error:

java.io.IOException: Server returned HTTP response code: 500 for URL:

Is this related to the actual call or request?

EDIT:

I added the following after trying to replicate the REST API client call:

    conn.setRequestMethod("POST");
    conn.setRequestProperty("User-Agent", "Mozilla/5.0 
    (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like     
     Gecko)   Chrome/41.0.2228.0 Safari/537.36");
    conn.setRequestProperty("Content-Type", 
     "application/x-www-form-urlencoded");
    conn.setRequestProperty("Accept","*/*");
    conn.setRequestProperty("Accept-Encoding","gzip,deflate");
    conn.setRequestProperty("X-API-Key", key);


    String input = "by=url" + "&";
    input = input + "url=" + videoName + "&";
    input = 
    input + "for=metadata,xxxx_metadata,xxxx_statistics,
     xxxx_comments" + "&";
    input = input + "policy=custom";
    input = input.replace(",", "%2C");
    input = URLEncoder.encode(input,"US-ASCII");

    OutputStream os = conn.getOutputStream();
    os.write(input.getBytes());
    os.flush();
    os.close(); 

But now I get the 422 error which is an issue with semantic errors. Any advice desperately needed.

Thanks

vbNewbie
  • 3,267
  • 14
  • 68
  • 150

0 Answers0