Frage

i have few application attached to new relic. when i enter my api key and hit SEND REQUEST i get my response in json format.

curl -X GET 'https://api.newrelic.com/v2/applications.json' \ -H 'X-Api-Key:<api key>' -i

this is what the request goes. i dont know what the above code is. well i need to read the returned json message in C# and may be then deserialize the json message.

i tried this

 public ActionResult Index()
    {
        WebRequest wr = WebRequest.Create("https://api.newrelic.com/v2/applications.json");

        wr.ContentType = "application/json";
        wr.Method = "GET";
        //wr.Headers["X-Parse-REST-API-Key"] = "<my api key>";
        wr.Headers.Add("Authorization", "<my api key>");

        using (WebResponse response = wr.GetResponse())
        {
            using (Stream stream = response.GetResponseStream())
            {
                int x = 10;
            }
        }

but i get 500 error.

War es hilfreich?

Lösung

Your code is very close to working. You just need to change your request header a bit as shown below (and substitute your own api key). Then, as you say you will need to deserialize the json. I've tested this bit of code and it returned the equivalent of the curl command.

    WebRequest wr = WebRequest.Create("https://api.newrelic.com/v2/applications.json");
    wr.ContentType = "application/json";
    wr.Method = "GET";
    wr.Headers.Add("X-Api-Key:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");

    using (WebResponse response = wr.GetResponse())
    {
        using (Stream stream = response.GetResponseStream())
        {
            byte[] bytes = new Byte[10000];
            int n = stream.Read(bytes, 0, 9999);
            string s = System.Text.Encoding.ASCII.GetString(bytes);
        }
    }

As you probably know, you can use our api explorer to form the http request needed to extract the data you are interested in. Then you should be able to copy the request from api explorer to your c# code. See the api explorer docs here: https://docs.newrelic.com/docs/features/getting-started-with-new-relics-api-explorer.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top