0

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?

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

2 Comments

I get a statuscode 400 saying that correct data must be passed. Is there a way to find out what data the API is expecting ?
Ok, that's a HTTP bad request error. There's no technical way to find out the expected input for a REST-api. You will have to search for the documentation I am afraid.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.