简体   繁体   中英

How to correctly convert from PHP and CURL post to C# HTTP Client post for groupon API

I've tried to convert the following code

<?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 {

      }
   }

to work with the HTTP client in C# by using the form url encoded content class as below in place of the $datatopost array in the PHP

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

and then using a http client to post this to the API however i get the below response

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

I assume it has something to do with the JsonConvert.SerializeObject(country.ci_lineitem_ids) from the Newtonsoft Json package I am using to convert from a array of strings to a json encoded array as it shows in the PHP code.

Anyone able to help with some ideas as to why this isn't working as expect as i'm all out of ideas at this now having tried multiple different ways to do this before this one?

Unless it's a typo, you forgot an s at the end of the third parameter in you c# code.

ci_lineitem_ids is in your php

ci_lineitem_id is in c#

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