1

I have an external API that is hosted on a different domain for now we can use the following URL as an example

https://myapi.mydomain/api/data

This API returns the following data:

{"data":[
    {
        "id":1,
        "company":"JUST A DEMO",
        "ext_identifier":"DEMO1"
    },
    {
        "id":2,
        "company":"ANOTHER DEMO",
        "ext_identifier":"DEMO2"
    }
 ]}

I need to call a controller method that does the GET request against this API and then returns the JSON data for me to consume.

So far I have the following code, I think I am close....

Here is the controller code

string url = "https://myapi.mydomain/";

    [HttpGet]
    public async Task<ActionResult> Search()
    {
        CustomerList customers = null;

        using (var client = new HttpClient())
        {
            client.BaseAddress = new Uri(url);
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            // New code:
            HttpResponseMessage response = await client.GetAsync("api/data");
            if (response.IsSuccessStatusCode)
            {
                customers = await response.Content.ReadAsAsync<CustomerList>();
            }
        }
        return Json(new
        {
            data = customers
        },
        JsonRequestBehavior.AllowGet);
    }

public class CustomerList
{
    public int id { get; set; }
    public string company { get; set; }
    public string ext_identifier { get; set; }
}
3
  • 1
    What is the problem statement? Commented Nov 17, 2016 at 2:10
  • Apologies I just was not getting any data back it looks like it was failing on the ReadAsAsync line Commented Nov 17, 2016 at 2:16
  • you want to read the json Data in your MVC Controller? Commented Nov 17, 2016 at 5:54

1 Answer 1

1

Why is it that when I ask a question 10 minutes later I come up with the answer so here it is if anyone is interested.

This seems to be the most elegant solution I can come up with but if anyone has any improvements please let me know thanks.

[HttpGet]
    public async Task<ActionResult> GetCustomers()
    {
        using (var client = new HttpClient())
        {
            client.BaseAddress = new Uri(url);
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            HttpResponseMessage response = await client.GetAsync(customerApi);
            if (response.IsSuccessStatusCode)
            {
                string jsondata = await response.Content.ReadAsStringAsync();
                return Content(jsondata, "application/json");
            }
            return Json(1, JsonRequestBehavior.AllowGet);
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.