0

I'm trying to use MailChimp API in an C# application.

I'm able to successfully use hurl.it to get the correct response so I know that it works.

I'm now trying to do this programmatically and cannot seem to either get it to work or know how to get the response and read it.

MailChimp always returns Json.

Here is the code I have so far:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://us10.api.mailchimp.com/3.0/Lists");
String username = "anystring";
String password = "my-api-key";
String encoded = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(username + ":" + password));
request.Headers.Add("Authorization", "Basic " + encoded);

HttpWebResponse response = (HttpWebResponse)request.GetResponse();

var rs = response.GetResponseStream();

Now when I use hurl.it it returns correctly:

{
    "lists": [{
        "id": "64df5g6h2",
        "name": "Agent List",
        "contact": {
            "company": "Test Agent",
            "address1": "123 Test Street",
            "address2": "",
            "city": "City",
            "state": "Ut",
            "zip": "84065",
            "country": "US",
            "phone": ""
        }
    }]
}

I'm stuck on how I can retrieve this same data through my code above? I'm not currently able to use any third-party libraries unless they are a part of the GAC, so no NuGet packages etc. It adds a layer of complication since I have to add them to the GAC to use them.

James Wilson
  • 5,074
  • 16
  • 63
  • 122
  • 1
    Well, you've got a stream... what's in the stream? You need to separate "fetching the data" from "parsing the JSON" - at the moment it's not clear which of those is really causing you a problem. – Jon Skeet Apr 26 '16 at 18:35
  • What is happening till now? Do you have a problem decoding the request into an object? What version of .net are you using? – Icepickle Apr 26 '16 at 18:35
  • So you need to parse the json string to an object without a third party usage, is that the question ? – Ismail Hawayel Apr 26 '16 at 18:50
  • @JonSkeet I'm not getting any errors. I'm just not sure how to grab the "body" out of my response. And when looking at the object through the debugger I don't see anything that really popsout with the json data. – James Wilson Apr 26 '16 at 19:00
  • @Icepickle I'm using .net 4. – James Wilson Apr 26 '16 at 19:00
  • Well you have a stream (`rs`) - you read from it. As a simple test, you could use `var text = new StreamReader(rs).ReadToEnd();` and see what's there. – Jon Skeet Apr 26 '16 at 19:01
  • @JonSkeet that worked, first time touching this part of .net so i was sure i was mising something. Is there a built in way to deserialize this without using a third party tool? – James Wilson Apr 26 '16 at 19:03
  • See the question that I've closed this one as the duplicate of. – Jon Skeet Apr 26 '16 at 19:04

0 Answers0