私はC#が初めてで、Chargifyでサブスクリプションを作成するPHPスクリプトを変換しようとしています( https://docs.chargify.com/api-introduction )。以下は私が使用しているコードです。テスト目的で JSON 文字列をハードコードしました。小さなねじれやエラーを解決して 1 日を過ごした後、ようやく 422 エラーが返されるようになりましたが、何が原因であるかを突き止めることはできません。JSON と C# に関する Stack Overflow の投稿をいくつか読みましたが、何も役に立ちませんでした。もっと経験のある人がこれを見て、私が見逃していることを指摘してくれることを願っています。この件に関する洞察をお寄せいただきありがとうございます。
[HttpPost]
public ActionResult Register(SignupViewModel regInfo)
{
string user = "xxxxxxxxxxxx";
string password = "x";
string jsonData = "{\"subscription\":{\"product_handle\":\"149-package\", \"coupon_code\" : \"\", \"customer_attributes\":{\"first_name\":\"Jerry\",\"last_name\":\"Jenkins\",\"email\":\"joe@example.com\"},\"credit_card_attributes\":{\"full_number\":\"1\",\"expiration_month\":\"10\",\"expiration_year\":\"2020\"}, \"components\" : [{\"component_id\" : 80012, \"enabled\" : false}, {\"component_id\" : 80014, \"enabled\" : true}]}}";
var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://o-asandbox.chargify.com/subscriptions.json");
var encodedStr = Convert.ToBase64String(Encoding.Default.GetBytes(string.Format("{0}:{1}", user, password)));
var authorizationKey = "Basic" + " " + encodedStr; // Note: Basic case sensitive
httpWebRequest.Headers.Add("Authorization", authorizationKey);
httpWebRequest.Method = "POST";
httpWebRequest.ContentType = "Content-Type: application/json";
byte[] byteArray = Encoding.UTF8.GetBytes(jsonData);
httpWebRequest.ContentLength = byteArray.Length;
Stream dataStream = httpWebRequest.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
return View();
}