参数发布

我试图对我的 MVC 3 API 进行一个非常基本的 REST 调用,我传递的参数没有绑定到 action 方法。

客户

var request = new RestRequest(Method.POST);


request.Resource = "Api/Score";
request.RequestFormat = DataFormat.Json;


request.AddBody(request.JsonSerializer.Serialize(new { A = "foo", B = "bar" }));


RestResponse response = client.Execute(request);
Console.WriteLine(response.Content);

服务器

public class ScoreInputModel
{
public string A { get; set; }
public string B { get; set; }
}


// Api/Score
public JsonResult Score(ScoreInputModel input)
{
// input.A and input.B are empty when called with RestSharp
}

我错过了什么吗?

270724 次浏览

你不需要自己连载尸体,就这么做

request.RequestFormat = DataFormat.Json;
request.AddJsonBody(new { A = "foo", B = "bar" }); // Anonymous type object is converted to Json body

如果您只想要 POST 参数(它仍然会映射到您的模型,而且因为没有序列化到 JSON 而更有效率) ,请这样做:

request.AddParameter("A", "foo");
request.AddParameter("B", "bar");

这是什么工作为我,对我的情况下,这是一个登录请求的帖子:

var client = new RestClient("http://www.example.com/1/2");
var request = new RestRequest();


request.Method = Method.POST;
request.AddHeader("Accept", "application/json");
request.Parameters.Clear();
request.AddParameter("application/json", body , ParameterType.RequestBody);


var response = client.Execute(request);
var content = response.Content; // raw content as string

身体:

{
"userId":"sam@company.com" ,
"password":"welcome"
}

在 RestSharp (105.2.3.0)的当前版本中,您可以通过以下方式向请求主体添加一个 JSON 对象:

request.AddJsonBody(new { A = "foo", B = "bar" });

此方法将内容类型设置为 application/JSON,并将对象序列化为 JSON 字符串。

如果您有一个对象的 List,您可以将它们序列化为 JSON,如下所示:

List<MyObjectClass> listOfObjects = new List<MyObjectClass>();

然后使用 addParameter:

requestREST.AddParameter("myAssocKey", JsonConvert.SerializeObject(listOfObjects));

您需要将请求格式设置为 JSON:

requestREST.RequestFormat = DataFormat.Json;

希望这能帮到别人,对我很管用

RestClient client = new RestClient("http://www.example.com/");
RestRequest request = new RestRequest("login", Method.POST);
request.AddHeader("Accept", "application/json");
var body = new
{
Host = "host_environment",
Username = "UserID",
Password = "Password"
};
request.AddJsonBody(body);


var response = client.Execute(request).Content;

以下是完整的控制台工作应用程序代码。请安装 RestSharp 包。

using RestSharp;
using System;


namespace RESTSharpClient
{
class Program
{
static void Main(string[] args)
{
string url = "https://abc.example.com/";
string jsonString = "{" +
"\"auth\": {" +
"\"type\" : \"basic\"," +
"\"password\": \"@P&p@y_10364\"," +
"\"username\": \"prop_apiuser\"" +
"}," +
"\"requestId\" : 15," +
"\"method\": {" +
"\"name\": \"getProperties\"," +
"\"params\": {" +
"\"showAllStatus\" : \"0\"" +
"}" +
"}" +
"}";


IRestClient client = new RestClient(url);
IRestRequest request = new RestRequest("api/properties", Method.POST, DataFormat.Json);
request.AddHeader("Content-Type", "application/json; CHARSET=UTF-8");
request.AddJsonBody(jsonString);


var response = client.Execute(request);
Console.WriteLine(response.Content);
//TODO: do what you want to do with response.
}
}
}

你可能需要在请求体中输入 Deserialize 的匿名 JSON 类型。

var jsonBody = HttpContext.Request.Content.ReadAsStringAsync().Result;
ScoreInputModel myDeserializedClass = JsonConvert.DeserializeObject<ScoreInputModel>(jsonBody);