使用 Square Connect 对卡 nonce 充值时出现 422 错误
422 error when charging the card nonce using Square Connect
我是 asp.net 开发人员。我使用 Square Connect Api 进行支付交易。下面是我的收费卡随机数代码。但我收到错误(不支持的媒体类型 \"application/x-www-form-urlencoded\",仅允许 [application/json])作为响应。解决方案表示赞赏。
方形连接请求:::
RestSharp.RestClient Client = new RestSharp.RestClient("https://connect.squareup.com");
RestSharp.RestRequest Request = new RestSharp.RestRequest("v2/locations/"+LocationId+"/transactions", RestSharp.Method.POST);
Request.RequestFormat = RestSharp.DataFormat.Json;
Request.AddHeader("Authorization", "Bearer " + access_token);
Request.AddHeader("Accept", "application/json");
Request.AddHeader("Content-Type", "application/json");
Request.AddParameter("name", "test");
Request.AddParameter("card_nonce", card_nonce);
Request.AddParameter("amount_money", "{\"amount\":100,\"currency\":\"USD\"}");
//Request.AddParameter("idempotency_key", Guid.NewGuid().ToString());
RestSharp.IRestResponse response = Client.Execute(Request);
System.Net.HttpStatusCode getresponse = response.StatusCode;
Square Up 的回复:
{"errors":[{"category":"INVALID_REQUEST_ERROR","code":"BAD_REQUEST","detail":"不支持的媒体类型\"application/x-www-form-urlencoded\",只允许 [application/json]"}]}
(公平警告我之前没有使用 RestSharp 的经验。)
似乎在创建带有正文(即 POST 或 PUT)的请求时,您必须使用 AddParameter
方法设置请求的内容类型,而不是 AddHeader
方法。
如 this answer 所示,当您的请求有主体时,您只需调用 AddParameter
一次:
Request.AddParameter("application/json", strJSONContent, ParameterType.RequestBody);
第一个参数指定内容类型,第二个参数是整个JSON你要POST的字符串,第三个参数表示JSON 字符串应该用作请求正文。
这意味着您需要在调用此方法之前构造您想要 POST 的 JSON 字符串。
我是 asp.net 开发人员。我使用 Square Connect Api 进行支付交易。下面是我的收费卡随机数代码。但我收到错误(不支持的媒体类型 \"application/x-www-form-urlencoded\",仅允许 [application/json])作为响应。解决方案表示赞赏。
方形连接请求:::
RestSharp.RestClient Client = new RestSharp.RestClient("https://connect.squareup.com");
RestSharp.RestRequest Request = new RestSharp.RestRequest("v2/locations/"+LocationId+"/transactions", RestSharp.Method.POST);
Request.RequestFormat = RestSharp.DataFormat.Json;
Request.AddHeader("Authorization", "Bearer " + access_token);
Request.AddHeader("Accept", "application/json");
Request.AddHeader("Content-Type", "application/json");
Request.AddParameter("name", "test");
Request.AddParameter("card_nonce", card_nonce);
Request.AddParameter("amount_money", "{\"amount\":100,\"currency\":\"USD\"}");
//Request.AddParameter("idempotency_key", Guid.NewGuid().ToString());
RestSharp.IRestResponse response = Client.Execute(Request);
System.Net.HttpStatusCode getresponse = response.StatusCode;
Square Up 的回复: {"errors":[{"category":"INVALID_REQUEST_ERROR","code":"BAD_REQUEST","detail":"不支持的媒体类型\"application/x-www-form-urlencoded\",只允许 [application/json]"}]}
(公平警告我之前没有使用 RestSharp 的经验。)
似乎在创建带有正文(即 POST 或 PUT)的请求时,您必须使用 AddParameter
方法设置请求的内容类型,而不是 AddHeader
方法。
如 this answer 所示,当您的请求有主体时,您只需调用 AddParameter
一次:
Request.AddParameter("application/json", strJSONContent, ParameterType.RequestBody);
第一个参数指定内容类型,第二个参数是整个JSON你要POST的字符串,第三个参数表示JSON 字符串应该用作请求正文。
这意味着您需要在调用此方法之前构造您想要 POST 的 JSON 字符串。