2

From an ASP.NET MVC app, I make a POST request to an action on my Web API. The action in the API executes successfully. When I make the same request in a REST client such as Postman for Chrome, I see a valid Json response.

However, if I make the call from my MVC app, I don't see any response back and the flow of control disappears after making the POST request to the API. The MVC app keeps waiting, as though.

Here's my code.

Web API

[HttpPost]
[ActionName("CreateNewUserFromAccountInfo")]
public IUser CreateNew(NewUserAccountInfo newUserAccountInfo)
{
    return _provider.CreateNew(newUserAccountInfo.FullName, newUserAccountInfo.Email, newUserAccountInfo.PasswordHash);
}

MVC app controller

IUser user = new WebAPIClient()
             .PostAsJsonAsync<NewUserAccountInfo, IUser>
              ("api/membership/CreateNewUserFromAccountInfo", 
              newUserAccountInfo
              ).Result;

From the WebAPIClient class from within a class library referenced from the MVC app:

public async Task<R> PostAsJsonAsync<T, R>(string uri, T value)
{
    using (var client = new HttpClient())
    {
        client.BaseAddress = new Uri(_baseUri);

        client.DefaultRequestHeaders.Accept.Clear();
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

        await PrintRequestBodyToDebugWindowAsync(value, new JsonMediaTypeFormatter());

        // The flow of control just disappears after this line
        // and never gets to the next line
        var response = await client.PostAsJsonAsync(uri, value);

        // The flow of control never gets here
        if (response.IsSuccessStatusCode) return await response.Content.ReadAsAsync<R>();
        else return default(R);
    }
}

RESULTS RETURNED WHEN REQUEST MADE VIA POSTMAN (a REST client)

Request Uri: http://localhost:54488/api/membership/CreateNewUserFromAccountInfo
Method: POST
Request Body: {"FullName":"Aamir Khan","Email":"aamir@khan.com","PasswordHash":"hello"}
Response:
{
    "Id": 15,
    "IsExternal": false,
    "ExternalId": null,
    "ExternalProviderName": null,
    "UserType": "Healthcare Seeker",
    "Verified": false,
    "VerificationCode": "375b8d2f-67df-433d-9e95-c84c30462b80",
    "VerificationCodeExpiresOn": "9999-12-31T23:59:59.9999999",
    "PasswordResetCode": null,
    "DefaultPasswordChanged": false,
    "HasFilledBasicProfile": false,
    "CreationDateTime": "2014-07-01T20:47:45.1405416+05:30",
    "FullName": {
        "Id": 2,
        "Name": "FullName",
        "Value": "Aamir Khan",
        "Description": null,
        "PrivacyLevel": {
            "Id": 1,
            "Name": "Private",
            "Description": "Visible only to me"
        }
    },
    "Email": {
        "Id": 1,
        "Name": "Email",
        "Value": "aamir@khan.com",
        "Description": null,
        "PrivacyLevel": {
            "Id": 1,
            "Name": "Private",
            "Description": "Visible only to me"
        }
    },
    "Profiles": [
        null,
        null,
        null
    ],
    "BasicProfile": null,
    "MedicalProfile": null,
    "EmergencyProfile": null
}
Water Cooler v2
  • 32,724
  • 54
  • 166
  • 336
  • 1
    Have you tried using Fiddler to inspect your HTTP request? – tia Jul 01 '14 at 15:52
  • @tia: Huh? I just posted a response I get from Postman. What's the point of doing it again in Fiddler? – Water Cooler v2 Jul 01 '14 at 15:53
  • @WaterCoolerv2: It would help you to know whether the browser is sending the request, and whether the browser *should be* getting the same response that you're getting via Postman. – StriplingWarrior Jul 01 '14 at 15:56
  • @StriplingWarrior: It's a POST request. I am not sure how you'd send one via a browser? – Water Cooler v2 Jul 01 '14 at 15:56
  • @WaterCoolerv2: "From an ASP.NET MVC app, I make a POST request to an action on my Web API." Clearly you're expecting your browser to send one. – StriplingWarrior Jul 01 '14 at 15:59
  • @StriplingWarrior: No, not the browser. The browser only send the HTTP request to the MVC app. The ASP.NET MVC server, i.e. the MVC app sends the POST request to the API using the `System.Net.HttpClient` class as you see in the `PostAsJsonAsync` method. – Water Cooler v2 Jul 01 '14 at 16:01
  • 1
    @WaterCoolerv2: Ah, that clarifies things a bit. FYI, it's not difficult to send an HTTP POST from a browser. Regardless, if you use Fiddler, you should be able to compare what the MVC app is sending and receiving with what Postman is sending and receiving. – StriplingWarrior Jul 01 '14 at 16:08
  • @StriplingWarrior: The MVC app just hangs and times-out, waiting for a response from the API, as though. The API, however, does return valid Json. BTW, just out of curiosity, how would you make a POST request from a browser? – Water Cooler v2 Jul 01 '14 at 16:21
  • http://stackoverflow.com/questions/3307379/how-to-send-a-post-request-with-a-web-browser – StriplingWarrior Jul 01 '14 at 16:29
  • That never occurred to me to do. I'll try that, thanks. I'm pretty sure I'll get back what Postman shows - a valid Json object. I'm quite certain I'm doing something stupid and that it has to do with deserialization. – Water Cooler v2 Jul 01 '14 at 16:32

0 Answers0