0

I am writing a test script in C# using NUnit framework where i send a httprequestmeassage to a uri to GET the __RequestVerificationToken in response. I wanted to know, how can i add that __RequestVerificationToken in the json and send it with a POST request. Do i need to deserialize the response and then add the __RequestVerificationToken or can i directly do it from the response object of HttpResponseMessage type? I am fairly new to C# and integration testing. i would really appreciate it if the answers would be simplified to the core. Thank You So Much! This is what i have come up with up until now:

[Test]
public void DoLogin()
        {
            string uri = "/account/login";
            AuthenticationRequest authenticationRequest = new AuthenticationRequest
            {
                Email = email,
                Password = password,
                ServiceType= ServiceType
            };

            HttpClient httpClient = new HttpClient { BaseAddress = new Uri(Consts.APIBaseURL) };
            httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            Task<HttpResponseMessage> responseResult = httpClient.GetAsync(uri);
            HttpResponseMessage httpResponseMessage = responseResult.Result;
            Console.WriteLine(httpResponseMessage.ToString());


            string jsonString = JsonConvert.SerializeObject(
                authenticationRequest,
                Formatting.Indented,
                new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() });

            HttpRequestMessage httpRequest = new HttpRequestMessage
            {
                RequestUri = new Uri(uri, UriKind.Relative),
                Method = HttpMethod.Post,
                Content = new StringContent(jsonString, Encoding.UTF8, "application/json")
            };

            //response message
            Task < HttpResponseMessage > response = httpClient.SendAsync(httpRequest);
            HttpResponseMessage httpResponseMessage = response.Result;
            Console.WriteLine(httpResponseMessage);

            //response content data
            HttpContent httpContent = httpResponseMessage.Content;
            Task <string> contentData = httpContent.ReadAsStringAsync();
            string data = contentData.Result;
            Console.WriteLine("Content Data: \n" + data);

        }
  • Does this answer your question? [How to retrieve Cookies from HttpResponseMessage](https://stackoverflow.com/questions/57060353/how-to-retrieve-cookies-from-httpresponsemessage) – mu88 Sep 28 '22 at 13:30
  • No. I don't think you understood my question correctly, i am trying to login into a Login method with [ValidateAntiForgeryToken] attribute. For which i have to send a GET request which returns a HttpResponse that has the cookie header with key "_requestVerificationToken" and value. I need to store the key and value and send it with the POST request method or JSON for the login to be successful. – Rahul Arya Oct 19 '22 at 09:10

0 Answers0