如何在RestSharp中发布数据?

时间:2014-11-21 09:28:18

标签: c# json restsharp

如何在RestSharp中发布数据?我对RestSharp很新,并且不太了解这个过程。

我熟悉Get Requests,我已经编写了API测试,没有任何问题。

使用以下示例我想发布订单数量:

                "name": "PostToCurrentBasket",
                "class": [
                    "PostToCurrentBasket"
                ],
                "method": "POST",
                "href": "*",
                "title": "Add Product to Current Basket",
                "type": "application/json",
                "fields": [
                    {
                        "name": "ProductId",
                        "type": "text",
                        "value": 101112,
                        "title": "Product ID"
                    },
                    {
                        "name": "Quantity",
                        "type": "number",
                        "value": 0,
                        "title": "Order Qty"
                    }
                ]
            }
        ],

到目前为止我所拥有的:

 var request = new RestRequest("baskets/current/", Method.POST);

我是否需要使用.AddBody,如果是,我该如何正确使用?

1 个答案:

答案 0 :(得分:1)

request.RequestFormat = DataFormat.Json; // If you want it to be json

request.AddBody(new { Name = "Foo", LastName = "Bar" }); // Any object that can be serialized

client.Execute(request);