0

I've got this piece of code that I am looping 100 times. As you can see below the number of loops is declared in the response I get from GET method. So it changes over time depending which number is inside the GET response body. Inside the body I also have "threads": 3 So i would like to bind the number for how many threads are needed for the POST method (below)

Is it possible to do it? I've never done it before & I don't know where to begin.

for (var i = 0; i < test.loop; i++)  //looping 100 times
{
    Console.WriteLine("Loop count: " + i.ToString());

    var newClient = new RestClient(url);

    var newRequest = new RestRequest(Method.POST);

    newRequest.AddHeader("Accept", "application/json");

    newRequest.AddHeader("Authorization", $"{testCases.header[0].auth}");


    newRequest.AddHeader("content-type", "application/json");

    newRequest.AddJsonBody(bodyRequest);

    var queryResult = newClient.Execute<object>(request);

    var content = JsonConvert.SerializeObject(queryResult.Data);

    Assert.IsTrue(content.Contains(testing.result.httpCode));
    Assert.IsTrue(content.Contains(testing.result.reponseAssert.ToString()));
}
Peter Csala
  • 17,736
  • 16
  • 35
  • 75
Mrks
  • 45
  • 1
  • 2
  • 14

1 Answers1

0

Example code for you ;

        public async Task<GetResponse> GetDetails(GetRequest getRequest)
        {
            GetResponse apiResponseClass = new GetResponse();

            var url = "your url";
            var client = new RestClient(url);
            var request = new RestRequest(url, Method.Post);
            request.AddHeader("Password", "xxxx");
            request.AddHeader("UserName", "yyyy");
            request.AddHeader("Content-Type", "application/json");

            var body = JsonConvert.SerializeObject(getRequest); 

            request.AddParameter("application/json", body, ParameterType.RequestBody);

            RestResponse response = await client.ExecuteAsync(request);
            var output = response.Content;
            
            return apiResponseClass;
        }
Mehmet
  • 65
  • 1
  • 7
  • 1
    I think you missed the mayor part of "doing it a 100 times with 3 request in parallel". You just show async code. That helps sometimes with multithreading but isn't the same. – Ralf May 12 '22 at 12:53
  • 1
    The code sample won't work as it ignores the documentation warning about setting the content-type header manually. – Alexey Zimarev May 14 '22 at 08:29