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
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
A Beginner’s Guide to HTTP and REST
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
- Default
Content-Type
(if not specified) with POST (default when you use-d
switch) isapplication/x-www-form-urlencoded
-
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:
-
Set the
Content-Type
toapplication/x-www-form-urlencoded
-
Add the Basic authentication
client.Authenticator = new HttpBasicAuthenticator("client-app", "secret");
-
Get rid of
request.AddParameter("client_id", "client-app"); request.AddParameter("client_secret", "secret");
-
Set the
Accept
header toapplication/json