如何使用 C# 将参数添加到 REST api?

How to add parameters to a REST api using C#?

我正尝试在 api 测试中使用 restsharp。 我需要以下代码的帮助,以使其正确。我试图通过在不同的测试中添加参数来做出响应。我尝试了不同的方法,但我是编程的初学者。

OrdersGate:

public class Authenticate
{
    public string userLogin { get; set; }
    public string authenticateKey { get; set; }
}

public class Client
{
    public string clientLogin { get; set; }
    public string clientFirstName { get; set; }
    public string clientLastName { get; set; }
    public string clientCity { get; set; }
    public string clientEmail { get; set; }
    public string clientHasTaxNumber { get; set; }
    public string clientSearchingMode { get; set; }
    public string clientFirm { get; set; }
    public string clientCountryId { get; set; }
    public string clientCountryName { get; set; }
}

public class OrdersDateRange
{
    public string ordersDateType { get; set; }
    public List<string> ordersDatesTypes { get; set; }
    public string ordersDateBegin { get; set; }
    public string ordersDateEnd { get; set; }
}

public class OrdersSerialNumberRange
{
    public int ordersSerialNumberBegin { get; set; }
    public int ordersSerialNumberEnd { get; set; }
}

public class OrdersRange
{
    public OrdersDateRange ordersDateRange { get; set; }
    public OrdersSerialNumberRange ordersSerialNumberRange { get; set; }
}

public class AuctionsAccount
{
    public int auctionsAccountId { get; set; }
    public string auctionsAccountLogin { get; set; }
}

public class AuctionsClient
{
    public string auctionClientId { get; set; }
    public string auctionClientLogin { get; set; }
}

public class AuctionsParams
{
    public List<string> auctionsServicesNames { get; set; }
    public List<int> auctionsItemsIds { get; set; }
    public List<AuctionsAccount> auctionsAccounts { get; set; }
    public List<AuctionsClient> auctionsClients { get; set; }
}

public class OrderSource
{
    public int shopsMask { get; set; }
    public List<int> shopsIds { get; set; }
    public AuctionsParams auctionsParams { get; set; }
}

public class Product
{
    public int productId { get; set; }
    public string productName { get; set; }
    public string sizeId { get; set; }
    public string sizePanelName { get; set; }
}

public class Packages
{
    public List<string> packagesNumbers { get; set; }
    public string orderHasPackageNumbers { get; set; }
}

public class Stock
{
    public int stockId { get; set; }
}

public class Campaign
{
    public int campaignId { get; set; }
    public List<string> discountCodes { get; set; }
}

public class OrdersBy
{
    public string elementName { get; set; }
    public string sortDirection { get; set; }
}

public class OrdersGet
{
    public string orderPrepaidStatus { get; set; }
    public List<string> ordersStatuses { get; set; }
    public List<string> couriersName { get; set; }
    public string orderPaymentType { get; set; }
    public List<string> withMissingSalesDocuments { get; set; }
    public string orderType { get; set; }
    public string dropshippingOrderStatus { get; set; }
    public List<string> ordersIds { get; set; }
    public List<int> ordersSerialNumbers { get; set; }
    public List<Client> clients { get; set; }
    public OrdersRange ordersRange { get; set; }
    public OrderSource orderSource { get; set; }
    public List<Product> products { get; set; }
    public int resultsPage { get; set; }
    public int resultsLimit { get; set; }
    public string clientRequestInvoice { get; set; }
    public Packages packages { get; set; }
    public List<Stock> stocks { get; set; }
    public Campaign campaign { get; set; }
    public string loyaltyPointsMode { get; set; }
    public string orderOperatorLogin { get; set; }
    public string orderPackingPersonLogin { get; set; }
    public List<OrdersBy> ordersBy { get; set; }
    public string searchingOperatorTypeMatch { get; set; }
    public string ordersDelayed { get; set; }
}

public class RootOrdersGet
{
    public Authenticate authenticate { get; set; }
    public OrdersGet @params { get; set; }
}

My code:

[TestFixture]
public class Get
{
    private static object[] DataOrdersGet =
    {
        new object[] { GlobalVariables.login, Modules.GenerateKey(Modules.HashPassword(GlobalVariables.pass)), "unpaid"},
        new object[] { GlobalVariables.login, Modules.GenerateKey(Modules.HashPassword(GlobalVariables.pass)), "restored"},
        new object[] { GlobalVariables.login, Modules.GenerateKey(Modules.HashPassword(GlobalVariables.pass)), "waiting"},
    };

    [OneTimeSetUp]
    public void BeforeAnyTests() { }

    [Test, TestCaseSource("DataOrdersGet")]
    public void SprawdzZamowieniaODanymStatusie(string login, string pass, string orderPrepaidStatus)
    {
        string vGate = "100";
        RestClient restClient = new RestClient(GlobalVariables.url + "api/?gate=orders/get/" + vGate + "/json");


        RestRequest restRequest = new RestRequest(Method.POST);

        var Authenticate = new Dictionary<string, string>
            {
                { "userLogin", login },
                { "authenticateKey", pass }
            };
        var Param = new Dictionary<string, string>
            {
                {"orderPrepaidStatus", orderPrepaidStatus}
            };

        var body = new
        {
            authenticate = Authenticate,
            @params = Param

        };

        restRequest.AddJsonBody(body);
        restRequest.AddParameter("application/json", body, ParameterType.RequestBody);

        IRestResponse restResponse = restClient.Execute(restRequest);

        Console.WriteLine("Status: " + restResponse.StatusCode);
        Console.WriteLine("\nResponse: " + restResponse.Content);
        Console.WriteLine("\nRequest: " + body.ToString());
    }

    [OneTimeTearDown]
    protected void CloseAfterAllTests() { }

现在,我正在通过字典添加参数。没有它如何让它变得更好?我知道有类似 restRequest.AddParameter() 的东西。但是我不知道怎么用,我的gate

您可以尝试这样做:

var body = new { 
    authenticate = new Authenticate() { 
        userLogin = login, 
        authenticateKey = pass} 
    },
    @params = new OrdersGet() {
        orderPrepaidStatus = orderPrepaidStatus
    }
}

// Adds the entire object to the request
// after serializing it and making it json
restRequest.AddJsonBody(body);

// Executes the request with the proper payload
IRestResponse restResponse = restClient.Execute(restRequest);

Console.WriteLine("Status: " + restResponse.StatusCode);
Console.WriteLine("\nResponse: " + restResponse.Content);
Console.WriteLine("\nRequest: " + body.ToString());