0

I am building a small Web API for syncing data and pulling down the objects works great, but pushing my objects up doesn't work no matter what I have tried.

Edited to reflect some changes:

Here is my Controller:

[System.Web.Mvc.HttpPost]
    public void UpdateTasks([FromBody] string s)
    {
        Console.WriteLine(s);
    }

Here is my Client code:

HttpContent c = new StringContent("1234");
        HttpClient client = new HttpClient();
        c.Headers.ContentType = new MediaTypeHeaderValue("application/json");
        client.BaseAddress = new Uri("http://localhost/QAQC_SyncWebService/Tasks/UpdateTasks/");
        var resp = client.PostAsync(client.BaseAddress, c).Result;

I can get a value though if I put it in the URI, but the string content alone doesn't seem to work.

Herrozerro
  • 1,601
  • 1
  • 22
  • 37
  • 1
    Set Content-Type: application/json, also use [FromBody] before string – neo Jan 15 '16 at 21:21
  • I did: c.Headers.ContentType = new MediaTypeHeaderValue("application/json"); and it's still null. – Herrozerro Jan 15 '16 at 21:22
  • Most probably your PUT request is blocked by webserver. Have a look at one of this threads: http://stackoverflow.com/questions/10906411/asp-net-web-api-put-delete-verbs-not-allowed-iis-8 http://stackoverflow.com/questions/28579073/how-to-get-put-and-delete-verbs-to-work-with-webapi-on-iis – serhiyb Jan 15 '16 at 21:30
  • Just as a dirty check, why don't you try using the full URL: `"http://localhost/QAQC_SyncWebService/Tasks/1234"` – Mark C. Jan 15 '16 at 21:31
  • @MarkC. I use the full url and that worked, only if I changed it to a Post action. – Herrozerro Jan 15 '16 at 21:39
  • @serhiyb that might be part of the issue, I changed it to a put and I was able to get data through when i used the full url with parameter. – Herrozerro Jan 15 '16 at 21:39
  • Have you tried [FromBody] before the string? – Bradley Thomas Jan 15 '16 at 21:41
  • @BradThomas Nope. Nothing. – Herrozerro Jan 15 '16 at 21:43
  • I added to my answer below, hope that might work for you. Sounds like you might need to adjust the body format slightly to get it to work this way – Bradley Thomas Jan 15 '16 at 21:58

2 Answers2

2

Try

[HttpPut]
public void UpdateTasks([FromBody]string s)
{
    Console.WriteLine(s);
}

Please also note:

  1. [FromBody] parameters must be encoded as =value The final hurdle remaining is that Web API requires you to pass [FromBody] parameters in a particular format. That’s the reason why our value parameter was null in the previous example even after we decorated the method’s parameter with [FromBody]. Instead of the fairly standard key=value encoding that most client- and server-side frameworks expect, Web API’s model binder expects to find the [FromBody] values in the POST body without a key name at all. In other words, instead of key=value, it’s looking for =value. This part is, by far, the most confusing part of sending primitive types into a Web API POST method. Not too bad once you understand it, but terribly unintuitive and not discoverable.

from http://encosia.com/using-jquery-to-post-frombody-parameters-to-web-api/

Bradley Thomas
  • 4,060
  • 6
  • 33
  • 55
1

The line you are initializing client.BaseAddress looks a bit off.

HttpContent c = new StringContent("1234");
HttpClient client = new HttpClient();
c.Headers.ContentType = new MediaTypeHeaderValue("application/json");
client.BaseAddress = new Uri("http://localhost/QAQC_SyncWebService/Tasks/UpdateTasks");
var resp = client.PutAsync(client.BaseAddress, c).Result;

The PutAsync method is expecting the full URI, not just a method. Read more here: https://msdn.microsoft.com/en-us/library/hh138168(v=vs.118).aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-1

Chad
  • 872
  • 8
  • 24
  • it really didn't change anything, it's still hitting the debug in the controller, but the parameter is null. Do parameters need to be named? – Herrozerro Jan 15 '16 at 21:28