如何正确地从PHP和CURL帖子转换为组上API的C#HTTP客户端帖子



我尝试转换以下代码

<?php
   // requires PHP cURL http://no.php.net/curl
   $datatopost = array (
      "supplier_id" => "1",
      "token" => "xYRPKcoakMoiRzWgKLV5TqPSdNAaZQT",
      "ci_lineitem_ids" => json_encode ( array (54553919, 54553920) ),
   );
   $ch = curl_init ("https://scm.commerceinterface.com/api/v4/mark_exported");
   curl_setopt ($ch, CURLOPT_POST, true);
   curl_setopt ($ch, CURLOPT_POSTFIELDS, $datatopost);
   curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
   $response = curl_exec ($ch);
   if( $response ) {
      $response_json = json_decode( $response );
      if( $response_json->success == true ) {
        //Successfully marked as exported (only items which are not already marked exported
      } else {
      }
   }
使用

C# 中的 HTTP 客户端,方法是使用表单 url 编码的内容类,如下所示来代替 PHP 中的$datatopost数组

FormUrlEncodedContent markExportedContent = new FormUrlEncodedContent(new[] {
    new KeyValuePair<string,string>("supplier_id",country.supplier_id),
    new KeyValuePair<string, string>("token",country.token),
    new KeyValuePair<string, string>("ci_lineitem_id", JsonConvert.SerializeObject(country.ci_lineitem_ids))
});

然后使用 http 客户端将其发布到 API,但我得到以下响应

{"reason": "Missing Parameters (ci_lineitem_ids).", "reason_code": 400, "success": false}

我认为这与我用来从字符串数组转换为 json 编码数组的 Newtonsoft Json 包中的JsonConvert.SerializeObject(country.ci_lineitem_ids)有关,如 PHP 代码所示。

任何人都可以帮助提出一些想法,为什么这没有按预期工作,因为我现在都不知道了,在此之前已经尝试了多种不同的方法来做到这一点?

除非是拼写错误,否则您忘记了 c# 代码中第三个参数末尾的 s。

ci_lineitem_ids在你的 php 中

ci_lineitem_id在 C# 中

最新更新