Here is my code
public static RestRequest CreateReportRequest()
{
restRequest = new RestRequest(Method.POST);
restRequest.AddHeader("Content-Type", "application/json");
restRequest.AddHeader("Authorization", "Basic T1NUAxNFVxeTIwag==");
using (StreamReader file =
File.OpenText(@"C:\Automation\APIAutomationSuite\APIAutomationSuite\TestData\CreateReport.json"))
using (JsonTextReader reader = new JsonTextReader(file))
{
JObject Body = (JObject)JToken.ReadFrom(reader);
}
restRequest.AddJsonBody(Body);
var response = client.Execute(restRequest);
return restRequest;
}
When I debug the code I am seeing the json request body value in "JObject Body" but when press F10 and move to the next statement (restRequest.AddJsonBody(Body);) the Body value becomes null. That throws a bad request error. Please help me with this.
Your Body
variable is declared inside inner scope of using
statement. Thus when you're trying to reference it outside of using
- it is undeclared.
You can move the end of your method completely into using
scope, which, I guess, will eliminate your issue. For example:
...
using (JsonTextReader reader = new JsonTextReader(file))
{
JObject Body = (JObject)JToken.ReadFrom(reader);
restRequest.AddJsonBody(Body);
var response = client.Execute(restRequest);
return restRequest;
}
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.