Note that there are some explanatory texts on larger screens.

plurals
  1. POConsuming WebAPI JSON
    primarykey
    data
    text
    <p>I'm trying to build some kind of RESTful-like API, I'm aware of that my first draft probably isn't anything near the real RESTful design pattern. However my real question is how should I consume my service using JSON?</p> <p>In my so called real world example I want my users to sign in via the service so I have this AuthenticationController</p> <pre><code>namespace RESTfulService.Controllers { public class AuthenticationController : ApiController { public string Get(string username, string password) { // return JSON-object or JSON-status message return ""; } public string Get() { return ""; } } } </code></pre> <p>Considering the increasing popularity with the technology I assumed that very little code would be needed for consuming the service. Do I really need to serialize the JSON manually with some kind of third party package like json.net? Beneath is my draft for the client</p> <pre><code>private static bool DoAuthentication(string username, string password) { var client = InitializeHttpClient(); HttpResponseMessage response = client.GetAsync("/api/rest/authentication").Result; if (response.IsSuccessStatusCode) { //retrieve JSON-object or JSON-status message } else { // Error } return true; } private static HttpClient InitializeHttpClient() { HttpClient client = new HttpClient(); client.BaseAddress = new Uri("http://localhost/"); // Add an Accept header for JSON format. client.DefaultRequestHeaders.Accept.Add( new MediaTypeWithQualityHeaderValue("application/json")); return client; } </code></pre> <p>How do I send JSON from service and how do I interpreting it on the client?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload