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

nicedev80 :

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 try it from c# using RestSharp package.

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);
Kira Hao :

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Spring - wrong encoding POST request with x-www-form-urlencoded

How to send post request with x-www-form-urlencoded body

Post a x-www-form-urlencoded request from React Native

Why does Axios send my POST request with Content-Type application/x-www-form-urlencoded when using a string as the request body?

Jersey client Post Request with x-www-form-urlencoded Fails

RestSharp defaulting Content-Type to application/x-www-form-urlencoded on POST

Swift - How to send POST request with "x-www-form-urlencoded" content-type

Swift 4 send POST request as x-www-form-urlencoded

How is Laravel decoding HTTP request body Content-Type: application/x-www-form-urlencoded when using api call

How do I get raw request body using servicestack with content-type set to application/x-www-form-urlencoded?

Postman and setting up a variable in the body of x-www-form-urlencoded request

Postman x-www-form-urlencoded returns null value for a POST request even though the input field is filled

my request failed when the post 'content-type' is application/x-www-form-urlencoded and * form field param= { <this is a json object>}

How to send a POST request with Content-Type "application/x-www-form-urlencoded"

Karate DSL: How to add specific key and value (XML) as x-www-form-urlencoded to body of request?

How to post request with spring boot web-client for Form data for content type application/x-www-form-urlencoded

How to send post request with content-type x-www-form-urlencoded android retrofit

Node.js Axios POST request with application/x-www-form-urlencoded format?

PYTHON: requests.post() how to send request_body encoded as application/x-www-form-urlencoded

How test Post request with custom object in content type application/x-www-form-urlencoded?

How to send post request with x-www-form-urlencoded body with loopj-async-http library

why can't Volley String request send Body parameters with POST method in x-www-form-urlencoded?

How to send x-www-form-urlencoded in a post request in webclient?

POST application/x-www-form-urlencoded Body to REST API using Jitterbit

how to post body x-www-form-urlencoded using webclient?

How to do a post request using FORM-DATA or x-www-form-urlencoded with Android?

Making a MarkLogic xdmp:http-post() request with x-www-form-urlencoded request body

How to post an array of values using `UrlFetchApp.fetch` with `application/x-www-form-urlencoded`?

How to sent Http POST request with application/x-www-form-urlencoded