0

I am a newbie to Mailgun and REST and need some help.

If I use the Mailgun provided code:

RestClient client = new RestClient();
client.BaseUrl = "https://api.mailgun.net/v2";
client.Authenticator = new HttpBasicAuthenticator("api", "xxxx");
RestRequest request = new RestRequest();
request.Resource = "/address/validate";
request.AddParameter("address", "me@mydomain.com");
return client.Execute(request);

How do I retrieve and process the response that the address is valid or not?

ctf0
  • 6,991
  • 5
  • 37
  • 46
  • Unlike forum sites, we don't use "Thanks", or "Any help appreciated", or signatures on [so]. See "[Should 'Hi', 'thanks,' taglines, and salutations be removed from posts?](http://meta.stackexchange.com/questions/2950/should-hi-thanks-taglines-and-salutations-be-removed-from-posts). – John Saunders Sep 19 '14 at 18:10

2 Answers2

1

This code works for me. I didn't use RESTClient and wrote my own code(which works perfectly fine)

[System.Web.Services.WebMethod]
    public static object GetEmailInfo(string UserName)
    {
        var http = (HttpWebRequest)WebRequest.Create("https://api.mailgun.net/v2/address/validate?address=" + UserName);
        http.Credentials = new NetworkCredential("api","public key");
        http.Timeout = 5000;
        try
        {
            var response = http.GetResponse();
            var stream = response.GetResponseStream();
            var sr = new StreamReader(stream);
            var content = sr.ReadToEnd();
            JSON.JsonObject js = new JSON.JsonObject(content);
            return Convert.ToBoolean(js["is_valid"]);
        }
        catch (Exception ex)
        {

        }
    }
Rajat
  • 410
  • 4
  • 19
0

First of You should never post private information such as your public key of such API

Just by using the amazing Postman Chrome app you can see the result of such request:

click here to see the image below in full resolution

enter image description here

and I'm sure, if you instead of return client.Execute(request); you do

var result = client.Execute(request);
return result;

and adding a breakpoint in the return you can inspect what is the object that is passed from the call... without testing, I'm sure you can convert result.Content (as it's where RestSharp appends the response content) into an object and use that object (or use the dynamic type).


now, testing your code in VS:

click here to see the image below in full resolution

enter image description here

you can then use the dynamic object like:

click here to see the image below in full resolution

enter image description here

public void GetResponse()
{
    var client = new RestClient();
    client.BaseUrl = "https://api.mailgun.net/v2";
    client.Authenticator = new HttpBasicAuthenticator("api", "pubkey-e82c8201c292691ad889ace3434df6cb");

    var request = new RestRequest();
    request.Resource = "/address/validate";
    request.AddParameter("address", "me@mydomain.com");

    var response = client.Execute(request);
    dynamic content = Json.Decode(response.Content);

    bool isValid = content.is_valid;
    string domain = content.parts.domain;
}

and treat the content of the response just like the json passed:

{
    "address": "me@mydomain.com",
    "did_you_mean": null,
    "is_valid": true,
    "parts": {
        "display_name": null,
        "domain": "mydomain.com",
        "local_part": "me"
    }
}
balexandre
  • 73,608
  • 45
  • 233
  • 342
  • I'm using: dynamic content = System.Runtime.Serialization.Json.Decode(response.Content); But am getting the error: Compiler Error Message: CS0234: The type or namespace name 'Decode' does not exist in the namespace 'System.Runtime.Serialization.Json' (are you missing an assembly reference?) – user2970010 Sep 19 '14 at 18:36
  • addded: using Newtonsoft.Json; and changed code to: dynamic content = JsonConvert.DeserializeObject(response.Content); Now it works – user2970010 Sep 19 '14 at 20:16
  • `Json.Decode` is from `System.Web.Helpers`. If the answer helped you in some way, mark it as correct answer and let others see that for your question, there's a correct answer. – balexandre Sep 20 '14 at 19:49