RestSharp post request - Body with x-www-form-urlencoded values

前端 未结 4 1945
抹茶落季
抹茶落季 2020-12-03 04:43

I am using postman and making an api post request where I am adding body with x-www-form-urlencoded key/values and it works fine in postman.

The issue arrises when I

相关标签:
4条回答
  • 2020-12-03 04:54

    in my case this is what worked

    req.AddParameter("client_id", "unigen-corporation", ParameterType.HttpHeader);
    req.AddParameter("grant_type", "client_credentials", ParameterType.GetOrPost);
    
    0 讨论(0)
  • 2020-12-03 04:56

    Personally, I found AddObject() method quite useful, and cleaner when you have so many parameters to add.

    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.AddObject(config);
    
            var res = client.Execute(req);
            return res;
        }
    
    0 讨论(0)
  • 2020-12-03 05:03

    this working for me, it was generator from postman

            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 讨论(0)
  • 2020-12-03 05:11

    I personally find this way to work better for me when sending Form-UrlEncoded data.

    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
    
            // Content type is not required when adding parameters this way
            // This will also automatically UrlEncode the values
            req.AddParameter("client_id",config.client_id, ParameterType.GetOrPost);
            req.AddParameter("grant_type",config.grant_type, ParameterType.GetOrPost);
            req.AddParameter("client_secret",config.client_secret, ParameterType.GetOrPost);
            req.AddParameter("scope",config.scope, ParameterType.GetOrPost);
            req.AddParameter("response_type",config.response_type, ParameterType.GetOrPost);
    
            var res = client.Execute(req);
            return;
    }
    

    Details on this parameter type can be found here: https://github.com/restsharp/RestSharp/wiki/ParameterTypes-for-RestRequest#getorpost

    0 讨论(0)
提交回复
热议问题