0

For example my rest client looks something like this:

 var client = new RestClient("test/oauth/token");
        var request = new RestRequest(Method.POST);
        request.AddHeader("content-type", "application/json");
        request.AddParameter("application/json", "{\"client_id\":\test123123123\",\"client_secret\":\"fsdfsdhkfsjdhf\",\"audience\":\"https://test.auth0.com/api/v2/\",\"grant_type\":\"client_credentials\"}", ParameterType.RequestBody);
        IRestResponse response = client.Execute(request);

The code above works but I wanted to convert this to HTTP client instead, just not sure how to do it

HJ1990
  • 385
  • 1
  • 4
  • 19
  • 2
    RestClient uses HttpClient underneath. You shouldn't be writing JSON by hand either. This code can be replaced with `HttpClient.PostAsJsonAsync("test/oauth/token",someObject);` where `someObject` an object with the properties you want. BUT there are OAuth clients that can be used instead of hand-coded calls – Panagiotis Kanavos Oct 25 '22 at 14:16
  • Yes, but what if I don't want to use RestClient since it is a third party library ? – HJ1990 Oct 25 '22 at 14:43
  • @HJ1990 then don't use it. There are a lot of examples how to use `HttpClient` (either directly or via `IHttpClientFactory`, the latter one preferable) to make requests. What is an exact problem? – Guru Stron Oct 25 '22 at 14:50
  • I already posted the single line needed to POST an object as JSON – Panagiotis Kanavos Oct 25 '22 at 15:08
  • Loosely related.. but you can see 2 answers (one as RestClient, one as HttpClient) here: https://stackoverflow.com/questions/38494279/how-do-i-get-an-oauth-2-0-authentication-token-in-c-sharp/53787718#53787718 – granadaCoder Oct 25 '22 at 15:12
  • I super agree with the "PostAsJsonAsync" comment. Stop creating json (or xml) with string manipulation. #please #beggingYou – granadaCoder Oct 25 '22 at 15:13
  • Seeing the content of your JSON, I think you might be interested in OAuth/OIDC library such as IdentityModel to help you implement it correctly. See https://identitymodel.readthedocs.io/en/latest/client/overview.html – GrayCat Oct 25 '22 at 15:20

1 Answers1

2

The are a lot of examples including official documentation on how to make requests with HttpClient. For example:

static async Task PostAsJsonAsync(HttpClient httpClient)
{
    using HttpResponseMessage response = await httpClient.PostAsJsonAsync(
        "todos", 
        new Todo(UserId: 9, Id: 99, Title: "Show extensions", Completed: false));

    response.EnsureSuccessStatusCode()
        .WriteRequestToConsole();

    var todo = await response.Content.ReadFromJsonAsync<Todo>();
    WriteLine($"{todo}\n");

    // Expected output:
    //   POST https://jsonplaceholder.typicode.com/todos HTTP/1.1
    //   Todo { UserId = 9, Id = 201, Title = Show extensions, Completed = False }
}

Currently the recommended pattern of setting up/creating/consuming HttpClient's is by using IHttpClientFactory:

Guru Stron
  • 102,774
  • 10
  • 95
  • 132