RestSharp.RestRequest.AddObject C# (CSharp) Method

AddObject() public method

public AddObject ( object obj ) : void
obj object
return void
		public void AddObject(object obj, params string[] whitelist) {
			// automatically create parameters from object props
			var type = obj.GetType();
			var props = type.GetProperties();

			foreach (var prop in props) {
				bool isAllowed = whitelist.Length > 0 && whitelist.Contains(prop.Name);

				if (isAllowed) {
					var propType = prop.PropertyType;
					var val = prop.GetValue(obj, null);

					if (val != null) {
						if (propType.IsArray) {
							val = string.Join(",", (string[])val);
						}

						AddParameter(prop.Name, val);
					}
				}
			}
		}

Usage Example

Example #1
7
    protected void Page_Load(object sender, EventArgs e)
    {
        var cl = new RestClient("http://demo.vivapayments.com/");
        //var cl = new RestClient("https://www.vivapayments.com/"); // production URL

        var req = new RestRequest("api/orders", Method.POST);

        req.AddObject(new OrderOptions()
        {
            Amount = 100    // Amount is in cents

        });

        string MerchantId = Guid.Parse("1c204c59-9890-499c-bf5d-31e80dcdbdfd").ToString();    // the merchant id is found in the self-care environment (developers menu)
        string Password = "******"; // the password is set in the self-care environment (developers menu)

        cl.Authenticator = new HttpBasicAuthenticator(MerchantId, Password);

        // Do the post 
        var res = cl.Execute<OrderResult>(req);

        // once the order code is successfully created, redirect to payment form to complete the payment
        if (res.Data.ErrorCode == 0)
        {
            Response.Redirect(this.vivaPaymentFormURL + res.Data.OrderCode.ToString());
        }
    }
All Usage Examples Of RestSharp.RestRequest::AddObject