Sending PUT request through HttpClient to Saucelabs not responding

I am trying to send a request PUTto update a Saucelabs job through their API . However, the following code hangs, and I'm not sure why.

using (var client = new HttpClient())
{
    var sessionId = Browser.Driver.GetSessionId();
    var uri = new Uri($"https://saucelabs.com/rest/v1/{Configuration.SauceUserName}/jobs/{sessionId}");
    var uriWithCred =
        new UriBuilder(uri)
        {
            UserName = $"{Configuration.SauceUserName}",
            Password = $"{Configuration.SauceAccessKey}"
        }.Uri;
    var payload = new StringContent($"{{\"name\":\"{TestMethodName}\"}}", Encoding.UTF8, "application/json");
    var request = new HttpRequestMessage
    {
        Method = HttpMethod.Put,
        RequestUri = uriWithCred,
        Content = payload
    };
    var response = client.SendAsync(request).Result;                
}

The following cUrl request is successful (of course, with credentials).

curl -X PUT -s -u <username>:<access-key> 
-d "{\"name\": \"test name\"}"
https://saucelabs.com/rest/v1/<username>/jobs/<job-id>

Why does this request freeze and what can I do to make it successful?

For reasons unrelated to the question, I cannot set the job name when configuring WebDriver features.

+4
source share
1 answer

Why does this request freeze and what can I do to make it successful?

, .Result client.SendAsync, , , .

, call async await.

public async Task CallAPIAsync() {    
    using (var client = new HttpClient()) {
        var sessionId = Browser.Driver.GetSessionId();
        var uri = new Uri($"https://saucelabs.com/rest/v1/{Configuration.SauceUserName}/jobs/{sessionId}");
        var uriWithCred =
            new UriBuilder(uri) {
                UserName = $"{Configuration.SauceUserName}",
                Password = $"{Configuration.SauceAccessKey}"
            }.Uri;
        var payload = new StringContent($"{{\"name\":\"{TestMethodName}\"}}", Encoding.UTF8, "application/json");
        var request = new HttpRequestMessage {
            Method = HttpMethod.Put,
            RequestUri = uriWithCred,
            Content = payload
        };
        var response = await client.SendAsync(request);

        var content = await response.Content.ReadAsStringAsync();
    }
}
+3

Source: https://habr.com/ru/post/1674012/


All Articles