Jamie Jamie - 1 year ago 239
JSON Question

Returning HttpResponseMessage from MVC HttpPost, with content of JSON

My first post on StackOverflow but I am a long time lurker! Hopefully I canget some help on an issue I'm working on.

I am trying to achieve the following. The code below is setting the content to be a JSON string which contains the following...

"access_token": "value1",
"expires_in": "value2",
"client_in": null,
"scope": "value3"

and here is the code which sends the HttpResponseMessage back to the client.

public HttpResponseMessage token(string grant_type,
string code,
string client_id,
string client_secret,
string redirect_uri)
WpOAuth2TokenRetValSuccessVM OA2_Success = new WpOAuth2TokenRetValSuccessVM();

OA2_Success.access_token = "value1";
OA2_Success.client_in = client_id;
OA2_Success.expires_in = "value2"; //...The number of seconds left in the lifetime of the token
OA2_Success.scope = "value3"; //...Each access token can have only 1 scope

HttpResponseMessage response = new HttpResponseMessage();
response.StatusCode = HttpStatusCode.OK;
string strJson = JsonConvert.SerializeObject(OA2_Success, Newtonsoft.Json.Formatting.Indented);

StringContent n = new StringContent(strJson, Encoding.UTF8, "application/json");
response.Content = n;

return response;

Now then, on the client side for the life of me I cannot get the JSON string back out of the content. Here is the code that I am using to read the content.

HttpClient httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

var postData = new List<KeyValuePair<string, string>>();
postData.Add(new KeyValuePair<string, string>(str_KEY1, str_VALUE1));
postData.Add(new KeyValuePair<string, string>(str_KEY2, str_VALUE2));
postData.Add(new KeyValuePair<string, string>(str_KEY3, str_VALUE3));
postData.Add(new KeyValuePair<string, string>(str_KEY4, str_VALUE4));
postData.Add(new KeyValuePair<string, string>(str_KEY5, str_VALUE5));

HttpContent content = new FormUrlEncodedContent(postData);

httpClient.PostAsync(strURI, content).ContinueWith(requestTask =>
// Get HTTP response from completed task.
HttpResponseMessage response = requestTask.Result;

// Check that response was successful or throw exception

// Read response asynchronously as string and write out
var responseValue = response.Content.ReadAsStringAsync();

string n = responseValue.Result;

var i = 0;

Now then, string n's content is as follows, but how do I get at the JSON???
Thanks all.

StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: System.Net.Http.StringContent, Headers:
Content-Type: application/json; charset=utf-8

Answer Source

Sounds like you figured out the problem, but it should be noted that since you're using Web API, there's no need to serialize the data and build the HttpResponseMessage yourself; let the framework do it for you:

public WpOAuth2TokenRetValSuccessVM token(string grant_type,
                                          string code,
                                          string client_id,
                                          string client_secret,
                                          string redirect_uri)
    return new WpOAuth2TokenRetValSuccessVM
        access_token = "value1",
        client_in = client_id,
        expires_in = "value2",
        scope = "value3"