RestSharp Introductoin and Usage

https://stackify.com/restsharp/

What is RestSharp? An Introduction to RestSharp’s Features and Functionality

RestSharp Features

The RestSharp library boasts some powerful features, making it a singularly unique tool that syncs admirably with RESTful architecture and helps in creating varied DotNet applications. Some of these features include:

  • Custom serialization and deserialization via ISerializer and IDeserializer.
  • Both synchronous and asynchronous requests
  • Automatic XML and JSON parsing, including fuzzy element name matching (“product_id” in XML/JSON will match C# property named ‘ProductId’)
  • Multipart file/form uploads
  • oAuth 1, oAuth 2, Basic, NTLM and Parameter-based Authentication
  • Support for features such as GET, PUT, HEAD, POST, DELETE and OPTIONS


Perhaps most impressive is RestSharp’s support for a number of environments including .NET 3.5+, Silverlight 4, Windows Phone 7, Mono, and MonoTouch

Simple Usages of HttpWebRequest and RestSharp with JSON.NET

httpWebRequest用于流式传输,可传输文件


RestSharp and the factory pattern - you really should

The factory pattern

The factory pattern is as simple as it gets, it is essentially creating a class responsible for creating a specific instance of a type.

The following is a simple example of a class factory for creating a RestClient:

?
public class RestClientFactory : IRestClientFactory
{
     public RestClient Create( string baseUrl)
     {
         return new RestClient(baseUrl);
     }
}

The following is an example of a class factory for creating a RestRequest:

?
public class RestRequestFactory : IRestRequestFactory
{
     public RestRequest Create( string url, Method method)
     {
         return new RestRequest(url, method);
     }
}

Whilst the examples are pretty simplistic, the power comes when the request or client changes and you only have to alter them in one place and your whole code base continues to work.

The following code shows how to use the factories (I've assumed you are familiar with using dependency injection):

?
public class MyExampleClass
{
     private IRestClientFactory restClientFactory;
     private IRestRequestFactory restRequestFactory;
 
     public MyExampleClass(IRestClientFactory restClientFactory, IRestRequestFactory restRequestFactory)
     {
         this .restClientFactory = restClientFactory;
         this .restRequestFactory = restRequestFactory;
     }
 
     public IRestResponse GetById( string id)
     {
         var restClient = restClientFactory.Create(configurationSettings.BaseUrl);
         var restRequest = restRequestFactory.Create( string .Format( "myendpoint/{0}" , id), Method.GET);
         var response = restClient.Execute(restRequest);
         return response;
     }
}

There you have it, a very simple example of encapsulation which could save you countless painful hours of refactoring.


https://code.tutsplus.com/tutorials/a-beginners-guide-to-http-and-rest--net-16340





See RFC 6749 - 4.4.2. Client Credentials - Access Token Request

Here is the basic (recommended) format of the request

 POST /token HTTP/1.1
 Host: server.example.com
 Authorization: Basic czZCaGRSa3F0MzpnWDFmQmF0M2JW
 Content-Type: application/x-www-form-urlencoded

 grant_type=client_credentials

The reason your cURL command works

  1. Default Content-Type (if not specified) with POST (default when you use -d switch) is application/x-www-form-urlencoded
  2. Default authentication type, if not specified, is Basic. This is achieved with

    -u username:password
    -or in your case-
    -u client-app:secret@example.com/myapi/oauth/token
    

    You could also specify the auth type with --basic or --digest

    Also, I thought the -u switch was needed. Haven't tested, but if it works for you, I guess it's not needed.

You can use the -v switch in your cURL command to see all the headers involved in the request.

RestSharp fix:

  1. Set the Content-Type to application/x-www-form-urlencoded

  2. Add the Basic authentication

    client.Authenticator = new HttpBasicAuthenticator("client-app", "secret");
    
  3. Get rid of

    request.AddParameter("client_id", "client-app");
    request.AddParameter("client_secret", "secret");
    
  4. Set the Accept header to application/json




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值