How Do I Pass Json Object From Fiddler To Webapi2 - c#

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.

Related

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.

Return json representations of data from WebAPI without strongly-typed IEnumerable

Having read a ton of different solutions and trying out different approaches (here, here, here, here, here), I am getting different (and unexpected) results from an API controller.
I need to return a datatable expressed as JSON, via a web api controller:
[IdentityBasicAuthentication]
[Authorize]
[RequireHttps]
[Route("Reports/Report1")]
public HttpResponseMessage Get()
{
DataTable dt = MyDAL.GetDataTable();
if(someValidationFailed){
Request.CreateResponse(HttpStatusCode.BadRequest, "Friendly error here");
}
return Request.CreateResponse(HttpStatusCode.OK, Newtonsoft.Json.JsonConvert.SerializeObject(dt));
}
In Fiddler, the output appears to be encoded into an escaped string:
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
"[{\"Site\":\"Headquarters\",\"Department\":\"HR\",\"FirstName\":\"Bob\",\"MiddleName\":null,\"Surname\":\"Fern\",\"EmployeeNumber\":\"444\"},
{\"Site\":\"Headquarters\",\"Department\":\"HR\",\"FirstName\":\"Alice\",\"MiddleName\":null,\"Surname\":\"Smith\",\"EmployeeNumber\":\"769\"}]"
However, the goal is to output plain Json (or XML if the client requests it):
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
[{"Site":"Headquarters","Department":"HR","FirstName":"Bob","MiddleName":null,"Surname":"Fern","EmployeeNumber":"444"},
{"Site":"Headquarters","Department":"HR","FirstName":"Alice","MiddleName":null,"Surname":"Smith","EmployeeNumber":"769"}]
Is there any way to do this without creating an IEnumerable<Person> object? The reason is that this class is specific to this one method. I also don't want to use a StringBuilder, because the DataTable may be extremely large and I've seen memory exceptions thrown previously.
I'm new to WebAPI, so the answer may be really simple, but a code sample I can ask further questions on would be very helpful.
Web API handles serialization for you, so you do not need to call JsonConvert.SerializeObject. That is why you are getting an escaped string as your output value. Just pass the datatable directly to CreateResponse. Web API will turn it into JSON or XML for you depending on what was sent in the Accept header of the request. (It uses Json.Net under the covers.)
return Request.CreateResponse(HttpStatusCode.OK, dt);
Use anonymous type. JSON support serialization of anonymous types.
var persons = datatable.AsEnumerable()
.Select(datarow => new
{
Site = datarow.Field<string>("Site")
});
return Request.CreateResponse(HttpStatusCode.OK,
Newtonsoft.Json.JsonConvert.SerializeObject(persons));

asp.net API post values received are null from postmaster

I'm writing a very basic asp .net api with a simple post method. The post parameters are returning null. I've tried various ways to get the method to return the object I passed in. I created a data transfer object and I've verified that the method is getting called. What else can I check ?
post master settings ----
url: /api/values
params: incidentID 4
params: incidentTitle 'this is some text'
Content-Type: application/json
// POST api/values
[Route("api/values")]
[HttpPost]
public HttpResponseMessage Post([FromBody]incidentDTO incid)
{
return Request.CreateResponse(incid);
}
I assume by postmaster you meant Postman.
If you are using Postman, use the following steps:
Under Header type Content-Type with a value of application/json
Select the raw tab and enter your data as follows:
{
"incidentId":4,
"incidentTitle":"this is some text"
}
Click on Send.
As the post data is being read from the body, there in no need to enter any values in URL params.

WCF Data Services - Custom POST method

I have created a WCF Data Service and it works fine. My Custom methods that are GET type methods work ok as well. The problem is in POST custom method.
The method looks like that:
[WebInvoke(Method = "POST")]
public string CustomMethod(string myParameter)
{
return "yes" + myParameter;
}
I also invoke:
config.SetServiceOperationAccessRule("CustomMethod", ServiceOperationRights.All);
Then in fiddler my request looks like that:
Method: POST
URL: http://localhost:1219/DataService.svc/CustomMethod
Reguest Headers:
User-Agent: Fiddler
Host: localhost:1219
Content-Length: 27
Content-Type: application/x-www-form-urlencoded
Request Body:
myParameter=parameter1value
The method gets called but the "myParameter" parameter is always null. What am I missing?
Thanks for your time.
Please refer to Section 10.4.1.3. Invoking an Action for OData 3.
Short story: The content type must be JSON.
If the invoke request contains any non-binding parameter values, the
Content-Type of the request MUST be 'application/json', and the
parameter values MUST be encoded in a single JSON object in the
request body.
I believe the way you are passing parameter 'myParameter' may be wrong.
you can try to consume your service using visual studio and then try to pass it.
one more question..
when you call the service as get service, is Content-Type is
Content-Type:application/x-www-form-urlencoded

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