简体   繁体   中英

restsharp how to add key value pair as parameter

I am trying to consume stripe.com api with restsharp, using the charge command

https://stripe.com/docs/api/php#create_charge

there's an opportunity to pass metadata as key value pairs but I don't seem to succeed

  const string baseUrl = "https://api.stripe.com/";
  const string endPoint = "v1/charges";
  var apiKey = this.SecretKey;

  var client = new RestClient(baseUrl) { Authenticator = new HttpBasicAuthenticator(apiKey, "") };
  var request = new RestRequest(endPoint, Method.POST);

  request.AddParameter("card", token);
  request.AddParameter("amount", wc.totalToPayForStripe);
  request.AddParameter("currency", "eur");
  request.AddParameter("description", wc.crt.cartid + " - " + wc.co.oid);
  request.AddParameter("metadata", "{cartid: " + wc.crt.cartid + ", oid: " + wc.co.oid + "}");
  request.AddParameter("statement_description", "# " + wc.crt.cartid);
  request.AddParameter("description", wc.crt.cartid + " - " + wc.co.oid);

Always getting the following error:

Invalid metadata: metadata must be a set of key-value pairs

Clearly I don't pass the key value pair the way I should but I can't find any restsharp documentation on that.

Anyone can help?

Try this:

  const string baseUrl = "https://api.stripe.com/";
  const string endPoint = "v1/charges";
  var apiKey = this.SecretKey;

  var client = new RestClient(baseUrl) { Authenticator = new HttpBasicAuthenticator(apiKey, "") };
  var request = new RestRequest(endPoint, Method.POST);

  request.AddParameter("card", token);
  request.AddParameter("amount", wc.totalToPayForStripe);
  request.AddParameter("currency", "eur");
  request.AddParameter("description", wc.crt.cartid + " - " + wc.co.oid);
  request.AddParameter("metadata[cartid]", wc.crt.cartid);
  request.AddParameter("metadata[oid]", wc.co.oid);
  request.AddParameter("statement_description", "# " + wc.crt.cartid);
  request.AddParameter("description", wc.crt.cartid + " - " + wc.co.oid);

For some reason HTTP Post requests can not accept key-value objects and must be sent in this type of format. This isn't a stripe restriction, but HTTP in general.

我认为这是在告诉您这样输入:

request.AddParameter("metadata", "[ { cartid: " + wc.crt.cartid + "} ,{ oid: " + wc.co.oid + " }]" );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM