Created
October 1, 2019 14:17
-
-
Save vpetkovic/d187517793ea5521fb46be406ec3bf38 to your computer and use it in GitHub Desktop.
using RestSharp
using HTTPClient
using WebClient
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using RestSharp; | |
using System.Net; | |
using System.Net.Http; | |
public static void useRestSharp(string url) | |
{ | |
var client = new RestClient(url); | |
var request = new RestRequest(Method.POST); | |
request.AddHeader("Content-Type", "application/json"); | |
request.AddParameter("undefined", APIRequest(), ParameterType.RequestBody); | |
IRestResponse response = client.Execute(request); | |
Console.WriteLine(response.Content); | |
} | |
public static async void useHTTPClient(string url) | |
{ | |
var httpClient = new HttpClient(); | |
var request = httpClient.PostAsync(url, new StringContent(APIRequest(), Encoding.UTF8, "application/json")).Result; | |
var content = await request.Content.ReadAsStringAsync(); | |
Console.WriteLine(content); | |
} | |
public static void useWebClient(string url) | |
{ | |
var webClient = new WebClient(); | |
webClient.Headers[HttpRequestHeader.ContentType] = "application/json"; | |
var response = webClient.UploadString(url, APIRequest()); | |
Console.WriteLine(response); | |
} | |
// Could be serialized JSON object using json.net or newtonsoft.json | |
public static string APIRequest() | |
{ | |
string source = @"C:\Users\vpetkovic\Desktop\NH example.txt"; | |
dynamic obj = new ExpandoObject(); | |
obj.username = "data"; | |
obj.password = "data"; | |
obj.fileName = Path.GetFileName(source); | |
obj.file = Convert.ToBase64String(File.ReadAllBytes(source)); | |
string apiRequest = "{\r\n \"username\": \"" + obj.username + "\",\r\n \"password\": \"" + obj.password + "\",\r\n \"fileName\": \"" + obj.fileName + "\",\r\n \"file\": \"" + obj.file + "\"\r\n}"; | |
return apiRequest; | |
} |
Hey! thanks a lot, you helped me too much! Hugs from Chile
Glad I could help 🙂
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi there! Sorry for late reply.
I am not using Auth0 but based on their documentation found here, you can see how to get token based on the authorization flow you are using. You can see what each flow returns as a response, and in a context of your example above response would be stored in
Content: System.Net.Http.HttpConnectionResponseContent
Now you want to deserialize that json response. To achieve that you can use any json (de)serializer but I think using using HttpClient extensions methods in this sense would be the simplest.
You can install it using package manager console
Install-Package System.Net.Http.Json
and reference it inusing
statement. Then you will need to replace line 23 withwhere
TokenResponse
is your expected response objectTo further simplify future requests you can install
IdentityModel
packageInstall-Package IdentityModel
and then store retrieved access token like so:Note that this gist is an example for using clients in very short lived console applications hence the class is
static
, for simplest and quickest of testing. Otherwise, if your use case is different then I suggest you to use IHttpClientFactory to implement resilient HTTP requests