RestSharp JSON参数发布

时间:2011-06-10 23:18:37

标签: c# json asp.net-mvc-3 rest restsharp

我正在尝试对我的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
}

我在这里错过了什么吗?

7 个答案:

答案 0 :(得分:191)

您不必自己序列化身体。只是做

request.RequestFormat = DataFormat.Json;
request.AddBody(new { A = "foo", B = "bar" }); // uses JsonSerializer

如果您只想要POST params(它仍然会映射到您的模型并且由于没有序列化到JSON而效率更高),请执行以下操作:

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

答案 1 :(得分:38)

在当前版本的RestSharp(105.2.3.0)中,您可以使用以下命令将JSON对象添加到请求正文中:

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

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

答案 2 :(得分:33)

这对我有用,对我来说这是一个登录请求的帖子:

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" 
}

答案 3 :(得分:1)

希望这会对某人有所帮助。它对我有用-

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;

答案 4 :(得分:0)

如果您有List个对象,则可以按照以下步骤将它们序列化为JSON:

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

然后使用addParameter

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

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

requestREST.RequestFormat = DataFormat.Json;

答案 5 :(得分:0)

这是完整的控制台工作应用程序代码。请安装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.
    }
  }
}

答案 6 :(得分:0)

您可能需要从请求正文中反序列化您的匿名 JSON 类型。

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