2

I'm trying to consume WebApi but I'm having issues. My 'IsSuccessStatusCode' is always false and I have 404 in response.

I have tried multiple methods but can't be able to do it correctly.

Constants:

const string baseUri = ""; // base url of API
const string setDealFlagUri = "Deals/SetDealFlag";

Method 1, using PostAsync:

using (var client = new HttpClient())
{
    client.BaseAddress = new Uri(baseUri);

    var content = new FormUrlEncodedContent(new[]
    {
        new KeyValuePair<string, string>("deadId", "3"),
        new KeyValuePair<string, string>("flagValueToSet", "true")
    });

    var response = await client.PostAsync(setDealFlagUri, content);
    if (response.IsSuccessStatusCode)
    {
        return true;
    }
}

Method 2, using PostAsJsonAsync:

using (var client = new HttpClient())
{
    client.BaseAddress = new Uri(baseUri);
    DealFlag content = new DealFlag
    {
        deadId = 3,
        flagValueToSet = true
    };

    var response = await client.PostAsJsonAsync(setDealFlagUri, content);
    if (response.IsSuccessStatusCode)
    {
        return true;
    }
}

WebApi request detail:

Curl:

curl -X POST --header 'Accept: application/json' '{baseApiurl}/Deals/SetDealFlag?dealId=3&flagValueToSet=true'

Request URL

{baseApiurl}/Deals/SetDealFlag?dealId=3&flagValueToSet=true

Response Body

{
  "Successful": true,
  "ErrorMessages": [],
  "ValidationResults": {
    "IsValid": false,
    "ValidationErrors": []
  }
}

Response Headers

{
  "pragma": "no-cache",
  "date": "Wed, 24 Aug 2016 18:38:01 GMT",
  "content-encoding": "gzip",
  "server": "Microsoft-IIS/8.0",
  "x-aspnet-version": "4.0.30319",
  "x-powered-by": "ASP.NET",
  "vary": "Accept-Encoding",
  "content-type": "application/json; charset=utf-8",
  "cache-control": "no-cache",
  "content-length": "198",
  "expires": "-1"
}

Please help me to use this webapi function correctly. Thanks!

11
  • What the problem? IsSuccessStatusCode is always false? If so what is in the response? Commented Aug 25, 2016 at 7:49
  • yes, 'IsSuccessStatusCode' is always false. and I have 404 in response. Commented Aug 25, 2016 at 7:50
  • @Saadi 404 - means NOT FOUND. So check your url first Commented Aug 25, 2016 at 7:53
  • URL seems ok for me. I have also added my url constants to my question. You can check it. It seems ok Commented Aug 25, 2016 at 7:54
  • @Saadi I'm not totally sure but as I can see you send your data via URL not as a form (using cURL). Did you try POST your request to the https://energydevdealswebservices20160719041846.azurewebsites.net/Deals/SetDealFlag?deadid=3&flagValueToSet=true url instead of sending data as a content to the https://energydevdealswebservices20160719041846.azurewebsites.net/Deals/SetDealFlag? Commented Aug 25, 2016 at 8:08

1 Answer 1

2

I think that the problem is that your controller method has signature like

[HttpPost]
public HttpResponseMessage SetDealFlag(int dealId, bool flagValueToSet)

Am I right? If your answer is "Yes" so your method wants parameters in the URL.

And so you get 404 error becouse no one of yours Web API methods matches to that URL.

Send your parameters dealId and flagValueToSet in the URL is the solution.

I wrote simple console app for testing my theory and it works perfectly:

public static void Main(string[] args)
{
  using (var client = new HttpClient())
  {
    try
    {
      // Next two lines are not required. You can comment or delete that lines without any regrets
      const string baseUri = "{base-url}";
      client.BaseAddress = new Uri(baseUri);

      var content = new FormUrlEncodedContent(new[]
      {
        new KeyValuePair<string, string>("deadId", "3"),
        new KeyValuePair<string, string>("flagValueToSet", "true")
      });

      // response.Result.IsSuccessStatusCode == true and no errors
      var response = client.PostAsync($"{baseUri}/Deals/SetDealFlag?dealId=3&flagValueToSet=true", null); 

      // response.Result.IsSuccessStatusCode == false and 404 error
      // var response = client.PostAsync($"{baseUri}/Deals/SetDealFlag", content); 
      response.Wait();
      if (response.Result.IsSuccessStatusCode)
      {
        return;
      }
    }
    catch (Exception ex)
    {
      Console.WriteLine(ex.Message);
      throw;
    }
  }
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.