RestSharp发布请求-具有x-www-form-urlencoded值的正文 [英] RestSharp post request - Body with x-www-form-urlencoded values

查看:1522
本文介绍了RestSharp发布请求-具有x-www-form-urlencoded值的正文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用邮递员并发出api投递请求,在其中添加带有x-www-form-urlencoded键/值的正文,并且在邮递员中工作正常.

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.

当我使用RestSharp软件包从c#中尝试该问题时,就会出现此问题.

The issue arrises when I try it from c# using RestSharp package.

我在下面尝试了以下代码,但没有得到响应.我收到"BadRequest" invalid_client错误.

I have tried the following code below but not getting the response. I get "BadRequest" invalid_client error.

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

推荐答案

这对我有用,它是邮递员的生成器

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

这篇关于RestSharp发布请求-具有x-www-form-urlencoded值的正文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆