Search code examples
asp.net-mvcsocrata

How to talk to OpenData API Endpoint without using sodaclient in mvc application


How can I talk to an OpenData API Endpoint (private dataset) in an MVC application without using sodaclient? I have an apptoken and credentials.


Solution

  • Just to clarify, it's a private dataset, correct?

    You'll need to make a RESTful HTTP call from your ASP.NET code, and include your application token as the X-App-Token header and provide HTTP Basic authentication with your user credentials.

    Here's some helpful documentation:

    SoQL is pretty straightforward, so you'll just need to construct the right SoQL query for your dataset and pass that in your GetAsync call. I am not a ASP.NET programmer, but I think it'd look something like this.

    using (var client = new HttpClient())
    {
        client.BaseAddress = new Uri("https://data.government.gov/");
        client.DefaultRequestHeaders.Accept.Clear();
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        // Also add X-App-Token and authentication headers here
    
        // New code:
        HttpResponseMessage response = await client.GetAsync("resource/644b-gaut.json?$where=date > '2014-12-01'");
        if (response.IsSuccessStatusCode)
        {
            // Do stuff
        }
    }
    

    Adding in the authentication and X-App-Token headers are left as an exercise to the reader.