Parkyster
Parkyster

Reputation: 155

How to post data in RestSharp?

How can I post data in RestSharp ? I am very new to RestSharp and dont quite understand the process.

I am familar with Get Requests, which I have written API tests for with no problems.

Using the below example I want to post the Order Qty:

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

What I have so far:

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

Do I need to be using .AddBody and if so how do I use this correctly?

Upvotes: 0

Views: 1342

Answers (1)

Emil Lundin
Emil Lundin

Reputation: 597

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);

Upvotes: 1

Related Questions