restsharp如何添加键值对作为参数

时间:2014-10-17 00:53:15

标签: c# stripe-payments restsharp

我正在尝试使用充电命令

使用restsharp来使用stripe.com api

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

有机会将元数据作为关键值对传递,但我似乎没有成功

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

始终收到以下错误:

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

显然,我没有按照我应该的方式传递密钥值对,但我无法找到任何关于它的restsharp文档。

任何人都可以提供帮助吗?

2 个答案:

答案 0 :(得分:4)

试试这个:

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

由于某些原因,HTTP Post请求无法接受键值对象,因此必须以此类格式发送。这不是条带限制,而是一般的HTTP。

答案 1 :(得分:0)

我认为它会告诉你这样输入它们:

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