How to get the test response in Google Chrome instead of Try out box?

Hi,

I want to see the response in JSON format in Google Chrome instead of the Try Out box on the Oxford Documentation page. All I need to know is how to append the app_key and app_id header in the URL.

Thanks,

Comments

  • AmosDuveenAmosDuveen Member, Administrator, Moderator admin

    Hi @shahood,

    How you specify headers very much depends on which programming language you are using. For example, the documentation page allows you to see example code in Objective-C, Swift, java, and Python. Also, there is a cURL example given when you click "try it out", although you need to change the hostname in the URL (remove the "-demo" part), the app_id, and the app_key to make it work from your own device; e.g. curl -X GET --header "Accept: application/json" --header "app_id: XXXXXXXX" --header "app_key: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" "https://od-api.oxforddictionaries.com:443/api/v1/entries/en/ace".

  • shahoodshahood Member ✭✭
    edited May 2018

    Hi @AmosDuveen ,

    Thanks for the reply!

    Pl note that I'm trying to use this API in my test android app and that I have achieved successfully. There I can retrieve the data perfectly fine. So, I know how to use the API with Java.

    The cURL thing is too technical for a beginner like me...I don't really know what to make of it.

    The issue is that I find it difficult to analyse the test data in that rather small window of Try Out, so I want to display it in google chrome.
    Now if I use "https://od-api.oxforddictionaries.com/api/v1/entries/en/ace" in google chrome, it returns "Authentication parameters missing".
    All I need to know is how to format the URL so that it could be pasted in Google Chrome to get the JSON response right there.

    Sorry if my original question wasn't clear enough!

    Thanks!

  • AmosDuveenAmosDuveen Member, Administrator, Moderator admin

    Hi @shahood,

    I think there has been something of a misunderstanding. The ODAPI output is not a web page that can be viewed in the same way as HTML and there is no functionality in a web browser which would allow you to enter the necessary credentials. Instead, you will need to write a program (your preferred programming language shouldn't matter), for example in Java, a basic program to retrieve a definition might look a bit like this (from the documentation page):

    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    
    //add dependencies to your class
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.net.URL;
    import javax.net.ssl.HttpsURLConnection;
    import android.os.AsyncTask;
    
    
    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            new CallbackTask().execute(dictionaryEntries());
        }
    
        private String dictionaryEntries() {
            final String language = "en";
            final String word = "Ace";
            final String word_id = word.toLowerCase(); //word id is case sensitive and lowercase is required
            return "https://od-api.oxforddictionaries.com:443/api/v1/entries/" + language + "/" + word_id;
        }
    
    
        //in android calling network requests on the main thread forbidden by default
        //create class to do async job
        private class CallbackTask extends AsyncTask<String, Integer, String> {
    
            @Override
            protected String doInBackground(String... params) {
    
                //TODO: replace with your own app id and app key
                final String app_id = "XXXXXXXX";
                final String app_key = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
                try {
                    URL url = new URL(params[0]);
                    HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection();
                    urlConnection.setRequestProperty("Accept","application/json");
                    urlConnection.setRequestProperty("app_id",app_id);
                    urlConnection.setRequestProperty("app_key",app_key);
    
                    // read the output from the server
                    BufferedReader reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
                    StringBuilder stringBuilder = new StringBuilder();
    
                    String line = null;
                    while ((line = reader.readLine()) != null) {
                        stringBuilder.append(line + "\n");
                    }
    
                    return stringBuilder.toString();
    
                }
                catch (Exception e) {
                    e.printStackTrace();
                    return e.toString();
                }
            }
    
            @Override
            protected void onPostExecute(String result) {
                super.onPostExecute(result);
    
                System.out.println(result);
            }
        }
    }
    
  • shahoodshahood Member ✭✭

    Hmm, I used The Guardian News API and there I was able to paste the required url in Chrome and get the output directly in there. So I thought there might as well be a way here.

  • AmosDuveenAmosDuveen Member, Administrator, Moderator admin

    Hi @shahood,

    APIs are just a method to send and retrieve data between different applications. There is a wide variation in setup with regard to what the data happens to be, how it is formatted, and how accessible it is.

    As it is, I think most web browsers will display raw JSON outputs, such as we provide, but they don't give you a way to enter the credentials to access our API because that is not their function.

  • shahoodshahood Member ✭✭

    In that case, can u pl work on a better display area for try out. In such a small area, one has to scroll up and down many times to find the right grand parent of a specific child field.
    Even if I use java, I would run the code on an Android device or the console of an IDE, that still doesn’t beat how we can view the output in a browser. In chrome, there are certain extensions that let u see formatted JSON right in the browser window with the option to expand or collapse various parent fields. That’s really helpful in understanding the data before starting to code.

  • AmosDuveenAmosDuveen Member, Administrator, Moderator admin

    Hi @shahood,

    The documentation page is just that, documentation. It isn't there to be used, just to give you a flavour of what is possible and how to construct requests.

    Firstly, have you signed up for an account yet? (if you have, you should have been sent an app_id and app_key).

    Secondly, leave the web pages out of it entirely and find a way to make a call from within a Java program (I copy/pasted one way of doing that earlier; remember to fill in your credentials)

    Once you have the result back from the API you then need to work out how to display it (presumably in some form of HTML template) and work out a way to suppress entries from the sex domain.

  • hsingh762hsingh762 Member

    Hello @shahood , I was having a similar issue and I found a way of it, just copy the results from the try out box and paste it in this formatter , you would be able to see a formatted output, you may get some misplaced brackets, which you can correct. let me know if it works for you.

  • shahoodshahood Member ✭✭

    @hsingh762 Thanks....it was helpful!

Sign In or Register to comment.