WebAPI passed post parameters is null - c#

I'm testing with WebAPI 2 and I have created the following controller method.
// POST api/values
public string Post([FromBody] string value)
{
string returnValue = "Return: " + value ;
return returnValue;
}
When i'm posting the following message with fiddler the method parameter keep returning null.
POST http://localhost:50814/api/Values/ HTTP/1.1
Host: localhost:50814
Content-Type: application/json
Content-Length: 14
{value: "New"}
I have simplyfied my code already as much as i can but still it stays null.
I think I'm overlooking something very simple but i'm out of ideas. Could someone please help me?
Thanks Sander

If you're taking a simple String from your Controller's Post method, try sending just this:
POST http://localhost:50814/api/Values/ HTTP/1.1
Host: localhost:50814
Content-Type: application/json
Content-Length: 10
"MyString"

Related

Web api behaves "stateful" in some successive calls from different client issue

I have a web api consumed by mobile apps. I can reproduce case with postman also with appropriate params.
Here are my postman call captured by fiddler:
GET http://localhost/WebApi/api/User/GetAnnouncement?id=22 HTTP/1.1
Content-Type: application/json
ApiKey: someKey
AuthenticationToken: someGuid1
UserId: 6524
DeviceId: someGuid2
LocalDate: 538294155.662561
OsTypeId: 1
LoginToken: someGuid3
CompanyId: 2
cache-control: no-cache
Postman-Token: b863afdd-b04c-4a4d-b473-69d5ecef622e
User-Agent: PostmanRuntime/7.4.0
Accept: */*
Host: localhost
cookie: ASP.NET_SessionId=nk4g3zzfyi0n3xomfw5dxxxx
accept-encoding: gzip, deflate
Connection: keep-alive
and my issue occurs in authorize action filter:
public class BasicAuthorizeAttribute : FilterAttribute
{
}
public class BasicAuthorizeFilter : AuthorizationFilterAttribute
{
public override void OnAuthorization(HttpActionContext actionContext)
{
//!!!HERE when I debug, in watch I can see already authHeader has value "in some calls"
System.Threading.Thread.SetData( System.Threading.Thread.GetNamedDataSlot("authHeader"), "someValueComingFromRequestHeader" );
}
}
At the very beginning of the OnAuthorization (see !!!HERE line in the code), I can see in watch this expression:
System.Threading.Thread.GetData(System.Threading.Thread.GetNamedDataSlot("authHeader"))
has the value even though I expect it is always null. It has even the value from previous client.
Actually issue come to as a bug "session" mingled (I mean mixed).
This pieces code is on the my company's framework so something is weirdly wrong.
I can give as much as I can so far. Please ask any info necessary.
What could be the cause?
I am daring to ask this because it is possible the issue obvious may be.
Note: I have the same case while none debugging with two phones connected to my pc via proxy settings.

ASP.NET Web API 2.0 Parameters Null

I know it's been asked a lot of time and I have tried everything but still facing the problem. Here is my code:
// POST api/<controller>
public string Post([FromBody]string value)
{
return value;
}
There is nothing fancy just returning the value.
I am using Chrome's PostMan Plugin
I have tried like 100 times but still getting the same null value as the response. I have tried it with Content-Type application/json and everything mentioned but still getting null.
Not really sure what the question is however what you are looking for is to setup your POST with a single value.
Such as below:
POST /api/values HTTP/1.1
Host: localhost:50121
Content-Type: application/json
Accept: application/json, text/javascript
Cache-Control: no-cache
'abc'
Now this is a JSON post so the body doesn't have a parameter name, this is by design as you are posting a single value.
If you need multiple values or a more complex object you should use a class object instead of a single value.

C# `[FromBody]` attribute bad unicode decoding for JSON

I have the following code,
[HttpPost("")]
public async Task<IActionResult> Create([FromBody] JToken body)
{
return Json(body);
}
I tried POSTing with and without unicode character, both responds with the exact document, but the unicode ones doesn't decode correctly in C# debugger (it does not display correctly), nor does it do anything useful (.ToString() prints nothing.)
My Request:
POST /api/vendors HTTP/1.1
Content-Type: application/json; charset=utf-8
Host: localhost:33000
Connection: close
User-Agent: Paw/2.2.5 (Macintosh; OS X/10.12.4) GCDHTTPRequest
Content-Length: 18
{
"A":"你好"
}
My Response:
{"A":"你好"}
Under Visual Studio, it shows:

How Do I Pass Json Object From Fiddler To Webapi2

http://localhost:15641/api/Complaints/NewComplaint
User-Agent: Fiddler
Content-Type: application/json
Host: localhost:15641
Content-Length: 63
RequestBody
{
"CostCentre":"test","ComplaintText":"This is test Complaint"
}
WebApi Controller
[Route("api/Complaints/NewComplaint")]
[HttpPost]
public void CreateNewComplaint(BLL.Complaint complaint)
{
//call BLL Create complaint method
}
}
my object is with null values
where am i doing wrong ?
i put breakpoints on controller the complaint object is set with all null values
if i don't pass the values complaint object it self is null
how can i pass the object
Web API expects the data sent to it to be serialized in a fashion like this:
Key1=Value1&Key2=Value2&...
If you are using JavaScript or JQuery, make sure that you're sending an Object(in your case matching Bll.Complaint), not a JSON string. If you give us more details on how you execute the request I will update this answer to have an example of how to do the serialization.

Dynamic model binding with ASP.NET WEB API

According to Scott Hanselman, on his blog, I should be able to do dynamic model binding and return a dynamic.
I have a Web API controller that contains a single method:
public dynamic Post(dynamic data)
{
return data;
}
When I make the following call from Fiddler, I am getting a null returned.
POST http://localhost:57856/api/values HTTP/1.1
User-Agent: Fiddler
Host: localhost:57856
Content-Type: "application/json"
Content-Length: 22
{"Name": "jlucpicard"}
What am I missing here? Shouldn't it return JSON for data? This is a simpler follow-up to my original question ASP.NET WEB API not binding to dynamic object on POST.
Your action is returning null because your "data" parameter is not being bound to the incoming json data.
Remove the quotes from "application/json" in your Content-Type header to bind to the data.
Content-Type: application/json

Categories