简体   繁体   中英

How to correctly translate RestSharp API call into non-RestSharp

I've been trying to update a legacy C# method that makes a call out to a remote API. Previously it was done using RestSharp, but it's not clear that that is necessary...except when I don't use it, it doesn't work correctly (the client returns a 404 instead of 200).

Test code looks like this:

        string remoteUrl = "https://thesite.com/v1/";
        var apiPath = "heres/a/path";

        // Without RestSharp
        HttpClient httpClient = new HttpClient();
        httpClient.BaseAddress = new Uri(remoteUrl);
        httpClient.Timeout = new TimeSpan(0, 0, 30);
        httpClient.DefaultRequestHeaders.Clear();
        httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", myAuthValue);
        var httpContent = new StringContent(stringPayload, Encoding.UTF8, "application/json");

        // This line returns a 404 from the client
        var response = await _httpClient.PostAsync(apiPath, httpContent);


        // With RestSharp
        var _client = new RestClient(remoteUrl);
        var request = new RestRequest
        {
            Resource = apiPath,
            Method = Method.POST,
            RequestFormat = DataFormat.Json
        };
        request.AddHeader("Authorization", $"Basic {myAuthValue}");
        request.AddParameter("application/json", stringPayload, ParameterType.RequestBody);

        // This line returns a 200 from the client
        var response2 = await _client.ExecuteAsync(request);

I feel like I must be doing something wrong either with the authorization, or adding the payload in the non-RestSharp version...but I have no idea what.

I've been trying to update a legacy C# method that makes a call out to a remote API. Previously it was done using RestSharp, but it's not clear that that is necessary...except when I don't use it, it doesn't work correctly (the client returns a 404 instead of 200).

Test code looks like this:

        string remoteUrl = "https://thesite.com/v1/";
        var apiPath = "heres/a/path";

        // Without RestSharp
        HttpClient httpClient = new HttpClient();
        httpClient.BaseAddress = new Uri(remoteUrl);
        httpClient.Timeout = new TimeSpan(0, 0, 30);
        httpClient.DefaultRequestHeaders.Clear();
        httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", myAuthValue);
        var httpContent = new StringContent(stringPayload, Encoding.UTF8, "application/json");

        // This line returns a 404 from the client
        var response = await _httpClient.PostAsync(apiPath, httpContent);


        // With RestSharp
        var _client = new RestClient(remoteUrl);
        var request = new RestRequest
        {
            Resource = apiPath,
            Method = Method.POST,
            RequestFormat = DataFormat.Json
        };
        request.AddHeader("Authorization", $"Basic {myAuthValue}");
        request.AddParameter("application/json", stringPayload, ParameterType.RequestBody);

        // This line returns a 200 from the client
        var response2 = await _client.ExecuteAsync(request);

I feel like I must be doing something wrong either with the authorization, or adding the payload in the non-RestSharp version...but I have no idea what.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM