Sending body object to request is not working

415 views
Skip to first unread message

Armando Cifuentes González

unread,
Mar 11, 2022, 11:11:23 PM3/11/22
to RestSharp
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 &#39;split&#39; 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!

Alexey Zimarev

unread,
Mar 13, 2022, 7:35:06 AM3/13/22
to RestSharp
AddObject is for forms, multipart or url-encoded. AddJsonBody is correct for objects that need to be serialized into JSON.

It is all documented, after reading your question I got an impression that you haven't spent time reading the docs https://restsharp.dev/usage.html#request-body

AddJsonBody works. If you have an issue with that, use a request tracer like https://requestbin.com to see how your requests look like. 

Reply all
Reply to author
Forward
0 new messages