如何使用C#RestSharp为对象发送HTTP Post?

时间:2014-06-26 13:25:42

标签: c# restsharp

我想在C#中使用Mockaroo REST API但是在JavaScript / Jquery中没有示例en C#。

有人可以建议如何发送带有以下(列)的HTTP Post作为请求参数。

var columns = [{
    name: "yearsEmployed",
    type: "Number",
    min: 1,
    max: 30,
    decimals: 0
}, {
    name: "department",
    type: "Custom List",
    values: ["R+D", "Marketing", "HR"]
}, {
    name: "dob",
    type: "Date",
    min: "1/1/1950",
    max: "1/1/2000",
    format: "%m/%d/%Y"
}];

var url = 'http://www.mockaroo.com/api/generate.json?key=abcd1234' +
  '&columns=' + encodeURIComponent(JSON.stringify(columns));

这是我的C#方法

var Mockarooclient = new RestClient("http://www.mockaroo.com/");
var Mockaroorequest = new RestRequest("api/generate.json", Method.POST);
Mockaroorequest.RequestFormat = DataFormat.Json;
Mockaroorequest.AddParameter("key", "abcd1234");

// How to pass Json parameters to RestSharp?
request.AddBody(new { ?????? }); 

Mockarooclient.Execute(Mockaroorequest);

1 个答案:

答案 0 :(得分:1)

我找到了解决方案。这是我的C#代码。

using System;
using System.Collections.Generic;
using System.Net;
using RestSharp;

namespace RestSharpMockaroo
{
    public class MockarooColumnsObject
    {
        public string name { get; set; }
        public string type { get; set; }
        public object min { get; set; }
        public object max { get; set; }
        public int decimals { get; set; }
        public List<string> values { get; set; }
        public string format { get; set; }
    }

    public class MockarooResponseObject
    {
        public string yearsEmployed { get; set; }
        public string department { get; set; }
        public string dob { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            List<MockarooColumnsObject> lMockarooColumnsObject = new List<MockarooColumnsObject>();
            lMockarooColumnsObject.Add(new MockarooColumnsObject() { name = "yearsEmployed", type = "Number", min = 1, max = 30, decimals = 0 });
            lMockarooColumnsObject.Add(new MockarooColumnsObject() { name = "department", type = "Custom List", values = new List<string> { "R+D", "Marketing", "HR" } });
            lMockarooColumnsObject.Add(new MockarooColumnsObject() { name = "dob", type = "Date", min = "1/1/1950", max = "1/1/2000", format = "%m/%d/%Y" });

            RestClient MockarooClient = new RestClient("http://www.mockaroo.com/");
            RestRequest MockarooRequest = new RestRequest("api/generate.json?key=abcd1234", Method.POST);
            MockarooRequest.RequestFormat = DataFormat.Json;
            MockarooRequest.AddBody(lMockarooColumnsObject);

            IRestResponse<MockarooResponseObject> response = MockarooClient.Execute<MockarooResponseObject>(MockarooRequest);
            if (response.StatusCode == HttpStatusCode.OK)
            {
                // OK 
                Console.WriteLine("yearsEmployed = {0}", response.Data.yearsEmployed);
                Console.WriteLine("department = {0}", response.Data.department);
                Console.WriteLine("dob = {0}", response.Data.dob);
            }
            else
            {
                // NOK
                Console.Write("ERROR: {0}", response.Content);
            }
        }
    }
}