2017-07-21 13 views
6

मैं डाकिया का उपयोग कर रहा हूं और एक एपीआई पोस्ट अनुरोध कर रहा हूं जहां मैं एक्स-www-form-urlencoded कुंजी/मानों के साथ शरीर जोड़ रहा हूं और यह डाकिया में ठीक काम करता है।RestSharp पोस्ट अनुरोध - एक्स-www-form-urlencoded मानों के साथ शरीर

समस्या तब होती है जब मैं इसे RestSharp पैकेज का उपयोग कर सी # से आज़माता हूं।

मैंने नीचे दिए गए कोड को आजमाया है लेकिन प्रतिक्रिया नहीं मिली है। मुझे "BadRequest" अवैध_client त्रुटि मिलती है।

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); 
+0

से जनरेटर मेरा उत्तर की जाँच करें, अगर यह आप के लिए उपयोगी है, स्वीकार किए जाते हैं जवाब के रूप में मार्क किया गया था। : डी –

उत्तर

4

मेरे लिए यह काम कर रहे हैं, यह डाकिया

 var token = new TokenValidation() 
     { 
       app_id = CloudConfigurationManager.GetSetting("appId"), 
       secret = CloudConfigurationManager.GetSetting("secret"), 
       grant_type = CloudConfigurationManager.GetSetting("grant_type"), 
       Username = CloudConfigurationManager.GetSetting("Username"), 
       Password = CloudConfigurationManager.GetSetting("Password"), 
     }; 

     var client = new RestClient($"{xxx}{tokenEndPoint}"); 
     var request = new RestRequest(Method.POST); 
     request.AddHeader("content-type", "application/x-www-form-urlencoded"); 
     request.AddParameter("application/x-www-form-urlencoded", $"app_id={token.app_id}&secret={token.secret}&grant_type={token.grant_type}&Username={token.Username}&Password={token.Password}", ParameterType.RequestBody); 
     IRestResponse response = client.Execute(request); 

     if (response.StatusCode != HttpStatusCode.OK) 
     { 
      Console.WriteLine("Access Token cannot obtain, process terminate"); 
      return null; 
     } 

     var tokenResponse = JsonConvert.DeserializeObject<TokenValidationResponse>(response.Content); 
+0

यह पैरामीटर espacping पैरामीटर को एन्कोड करने के लिए प्रतीत नहीं होता है। यह पोस्टमैन की तुलना में उद्धरणों का एक अतिरिक्त सेट भी जोड़ता है। – John

+0

RestSharp.Extensions.StringExtensions.UrlEncode विधि अपेक्षित में भेजे जाने से पहले मानों को एन्कोड कर सकता है। – hmadrigal

संबंधित मुद्दे