简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM