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);
}