1

I've tried implementing the simplest case of this with a call to Mandrill's API using the method "ping." The response should be pong. Using a valid key, the response seems to work. However, I am having trouble accessing the content of the response.

The code is as follows:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Http;
using System.Net.Http.Headers;

namespace MandrillAPI
{
    class Program
    {
        static void Main(string[] args)
        {
            string ping = "https://mandrillapp.com/api/1.0/users/ping.json";
            string key = "?key=123";
            HttpClient client = new HttpClient();
            client.BaseAddress = new Uri(ping);
            HttpResponseMessage response = client.GetAsync(key).Result;
            Console.WriteLine(response.ToString());
            Console.Read();
        }
    }
}

How do I unpack the response from the call? Ultimately, I need to harvest emails from the server using the API, so I'll need to somehow preserve the structure of the json objects so I can access the details of the email.

Community
  • 1
  • 1
Chris
  • 28,822
  • 27
  • 83
  • 158

2 Answers2

1

If you just want to read the response as a string:

string content = response.Content.ReadAsStringAsync().Result

If you want to deserialize to some class (in this case the type MyClass):

MyClass myClass = JsonConvert.DeserializeObject<MyClass>(response.Content.ReadAsStringAsync().Result)
peco
  • 3,890
  • 1
  • 20
  • 27
0

I would use something like JSON.Net to serialize it to a C# object that you can then use.

bwighthunter
  • 137
  • 2
  • 12