1

I'm using RestSharp to make calls to a REST API:

var client = new RestClient("http://mysite.com");        

var request = new RestRequest("/api/order", Method.POST);
request.AddHeader("AuthPass", "abcdefg1234567");

// add parameters here        

var response = client.Execute(request);
var content = response.Content;

I need to add parameters to the request. One is just my name, which is a string. The other is the list of orders items, which needs to be JSON in this format:

[
    {"SKU":"ABC-123", "QUANTITY":1},
    {"SKU":"XYZ-123", "QUANTITY":3}
]

I can add my name as a parameter like this:

request.AddParameter("name", "My Name");

But I don't know how to add the list of ordered items:

request.AddParameter("orderedItems", "???");

Anyone know how I can do this?

1 Answer 1

1

If you make a class like this:

public class Orders
{
    public string SKU { get; set; }
    public string QUANTITY { get; set; }
}

then, you can make a list like so:

List<Orders> orderList = new List<Orders>
    {
        new Orders {QUANTITY = "1", SKU = "ABC-123"},
        new Orders {QUANTITY = "3", SKU = "XYZ-123"}
    };

and finally:

request.AddParameter("OrderList", orderList );
Sign up to request clarification or add additional context in comments.

1 Comment

I agree, except that the order list should be in the body, not in the parameters. Use request.AddBody(orderList);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.