I'm trying to send a body object to a
POST request. If I send the following anonymous object (
parameters) to the request, it works:
public async Task<RestResponse> PostSessionAccountAsync()
{
var parameters = new
{
full_name = "Armando Cifuentes",
lang = "es-MX",
document_country = "MX",
document_type = "ID_CARD",
additionalData = new { isTest = false }
};
RestRequest postSessionRequest = new RestRequest(DemoSessionAPIClient, Method.Post);
postSessionRequest.AddObject(parameters);
RestResponse createResponse = await restClient.ExecutePostAsync(postSessionRequest);
CreatedSessionModel values = JsonConvert.DeserializeObject<CreatedSessionModel>(createResponse.Content);
SessionToken = values.SessionToken;
return createResponse;
}
However, if I attempt to send the following parameters object, made using a model, in the AddObject() method, it doesn't work:
namespace Demo.Tests.API.Data
{
public class TestObjects
{
public static PostSessionBodyModel parameters = new PostSessionBodyModel()
{
FullName = "Armando Cifuentes",
Language = "es-MX",
DocumentCountry = "MX",
DocumentType = "ID_CARD",
AdditionalData = new PostSessionBodyAdditionalDataModel { IsTest = false }
};
}
}
// using the object
postSessionRequest.AddObject(TestObjects.parameters);When I debug, it throws me the following error in the Content when attempts to create the request:
"<h1>Cannot read property 'split' of undefined</h1>\n<h2></h2>\n<pre></pre>\n<pre></pre>\n<pre></pre>\n"createResponse:
"StatusCode: InternalServerError, Content-Type: text/html, Content-Length: 105)"
The error itself when I just only run the test:
Newtonsoft.Json.JsonReaderException : Unexpected character encountered while parsing value: <. Path '', line 0, position 0.
These are the model classes that I'm using to create the body object:
namespace Demo.Tests.API.Models
{
public class PostSessionBodyModel
{
[JsonProperty("full_name")]
public string FullName { get; set; }
[JsonProperty("lang")]
public string Language { get; set; }
[JsonProperty("document_country")]
public string DocumentCountry { get; set; }
[JsonProperty("document_type")]
public string DocumentType { get; set; }
[JsonProperty("additionalData")]
public PostSessionBodyAdditionalDataModel AdditionalData { get; set; }
}
}
and
namespace Demo.Tests.API.Models
{
public class PostSessionBodyAdditionalDataModel
{
[JsonProperty("isTest")]
public bool IsTest { get; set; }
}
}
My body object looks like this:
{
"full_name":"Cindelyn Rhodie",
"lang":"es-MX",
"document_country":"MX",
"document_type":"ID_CARD",
"additionalData":
{
"isTest":false
}
}I tried using AddJsonBody() and AddBody() methods. Also, I serialized the object and I added the content-type header to the request, without success. Is there something that am I missing?
Thanks in advance for your help!