Tôi đang sử dụng người đưa thư và thực hiện yêu cầu đăng api nơi tôi thêm nội dung có khóa/giá trị x-www-form-urlencoded và hoạt động tốt trong người đưa thư.Yêu cầu bài đăng RestSharp - Nội dung có giá trị x-www-form-urlencoded
Sự cố xảy ra khi tôi thử từ C# bằng gói RestSharp.
Tôi đã thử mã dưới đây nhưng không nhận được phản hồi. Tôi gặp lỗi invalid_client "BadRequest".
public class ClientConfig {
public string client_id { get; set; } = "value here";
public string grant_type { get; set; } = "value here";
public string client_secret { get; set; } = "value here";
public string scope { get; set; } = "value here";
public string response_type { get; set; } = "value here";
}
public void GetResponse() {
var client = new RestClient("api-url-here");
var req = new RestRequest("endpoint-here",Method.POST);
var config = new ClientConfig();//values to pass in request
req.AddHeader("Content-Type","application/x-www-form-urlencoded");
req.AddParameter("application/x-www-form-urlencoded",config,ParameterType.RequestBody);
var res = client.Execute(req);
return;
}
//Also tried this
req.AddParameter("client_id",config.client_id,"application/x-www-form-urlencoded",ParameterType.RequestBody);
req.AddParameter("grant_type",config.grant_type,"application/x-www-form-urlencoded",ParameterType.RequestBody);
req.AddParameter("client_secret",config.client_secret,"application/x-www-form-urlencoded",ParameterType.RequestBody);
req.AddParameter("scope",config.scope,ParameterType.RequestBody);
req.AddParameter("response_type",config.response_type,"application/x-www-form-urlencoded",ParameterType.RequestBody);
//tried this too
var client = new RestClient("url-here");
var req = new RestRequest("endpointhere",Method.POST);
var config = new ClientConfig();
req.AddBody(config);
var res = client.Execute(req);
kiểm tra câu trả lời của tôi, nếu nó là hữu ích cho bạn, đánh dấu là câu trả lời chấp nhận. : D –