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

前端 未结 4 1961
抹茶落季
抹茶落季 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 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

提交回复
热议问题