Reputation: 239
I Have created a web api project , in values controller i created a method InsertHeading which takes three parameters and returns back a unique id. The method looks like this :-
public int InsertHeading([FromBody]string appid, [FromBody]string type, [FromBody]string detail)
{
int x = 1;
return 1;
}
I tried this variant as well
[HttpPost]
public int InsertHeading(string appid, string type, string detail)
{
int x = 1;
return 1;
}
This piece of code is running when i give url like :- http://server:port/LoggingAPi/Values/InsertHeading from soap UI.
But when i try to call this method from my c# code i am getting 404 error how ever i try. Here are two ways i have tried :-
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("http://xxxxx:45422/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var append = new MasterLogInfo() { appid = "2", type = "request", detail = "test call from genesys" };
HttpResponseMessage response = await client.PostAsJsonAsync("LoggingAPI/Values/InsertMasterloginfo", append);
if (response.IsSuccessStatusCode)
{
// Get the URI of the created resource.
Uri finalURL = response.Headers.Location;
}
}
Method 2:-
// client.BaseAddress = new Uri("http://localhost:53117/");
client.BaseAddress = new Uri("http://xxxxxx:45422/");
client.DefaultRequestHeaders.Accept.Clear();
// client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
var user = new MasterLogInfo();
user.appid = "100";
user.typeRequest = "Test";
user.detail = "test call from genesys";
var response = client.PostAsync("LoggingAPI/Values/InsertMasterloginfo", new StringContent(new JavaScriptSerializer().Serialize(user), Encoding.UTF8, "application/json")).Result;
// var response = client.PostAsync("Values/InsertHeading", new StringContent(new JavaScriptSerializer().Serialize(user), Encoding.UTF8, "application/json")).Result;
if (response.IsSuccessStatusCode)
{
// Get the URI of the created resource.
Uri finalURL = response.Headers.Location;
}
}
If i use FromBody tag in parameters i get 500 internal server error, without it i get 404 error. Can anybody tell me what am i missing.I have removed the body of insert Heading for security purpose
Upvotes: 1
Views: 715
Reputation: 392
Create an object that represents the payload coming in from the post. Then you can use it in the action parameters e.g. public int InsertHeading([FromBody] MyObject myObject)
Upvotes: 1