Tuga Lps Tuga Lps - 4 months ago 6x
JSON Question

Setting up a REST Client Application (Post) from a Web API

I've been having a few issues in trying to retrieve the results of a POST operation from a Web Service.
I have been using a chrome extension to test the API Services and they are working there. However I've been having problems on implementing it in code.

This is an example of usage of the chrome extension:


What I'm trying to retrieve on code, is the last part, the json array that the POST operation generates, where it says accessToken.

However, in the code that I've been using below, I've only had access to the status (200 OK) etc.

Here's a preview of the code I am using:

HttpClient client = new HttpClient();

client.BaseAddress = new Uri(url.Text);

client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(header.Text));

HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, url.Text);

request.Content = new StringContent(body.Text, Encoding.UTF8, header.Text);

.ContinueWith(responseTask =>

The Header.Text is exactly "application/json", the body.Text is body which has those various properties such as username and password (in string format) and url.Text contains the complete URL to call the Web service.

I'd like to know what I'm doing wrong with my code, and what can I do to obtain that json array that contains the accessToken


In your code you need to use ReadAsStringAsync method to convert your HttpContent object to string/json. For example:

      .ContinueWith(responseTask =>
          var jsonString = responseTask.Result.Content.ReadAsStringAsync().Result;

then you can convert you jsonString as you need.