问题规格:
资源URI:
address/index.php?r=api/employee
请求标头:
Content- Type: application/json
HTTP方法:
POST
要求正文:
{ "employeeName" : "ABC","age":"20","ContactNumber": "12341234"}
以上参数应作为JSON字符串中的HTTP POST行传递给系统。
我正在尝试使用解决此问题RESTSharp
。但是我遇到了一些问题,例如在执行请求后,我的代码返回了Null响应,并且我不确定我的JSON字符串是否正确传递。
这是我的代码:
public ActionResult EmployeeInfo(Employee employee)
{
//string empName = unSubscription.employeeName.ToString();
var client = new RestClient("http://localhost:21779/");
var request = new RestRequest("api/employee ", Method.POST);
request.RequestFormat = DataFormat.Json;
request.AddBody(new Employee
{
employeeName = "ABC",
age = "20",
ContactNumber = "12341234"
});
request.AddHeader("Content-Type", @"application/json");
// execute the request
IRestResponse response = client.Execute(request);
var content = response.Content; // raw content as string
return View();
}
我的代码有什么问题吗?
我有点困惑
request.AddUrlSegment("username", "Admin") and request.AddParameter("name", "value").
基本上我想知道如何利用AdduUrlSegment()
和AddParameter()
。
提前致谢。
使用时,request.AddUrlSegment("username", "Admin")
您应该正确定义url模板:var request = new RestRequest("api/employee/{username} ", Method.POST);
另外你应该设置 Content-Type
request.AddHeader("Content-Type", @"application/json");
在添加主体之前
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句