Whats the alternative for deprecated method 'AddBody' incase I want to send List<string>as body

472 views
Skip to first unread message

partha ss

unread,
Sep 12, 2019, 3:17:25 AM9/12/19
to RestSharp
I have to test a API which accepts List<String> val as body.If I use "request.addBody" everything works fine,But If i use addJsonbody it API throws exception that collection cant be null. S how to add a List<string> to body in request as alternative for addbody since this is deprecated.

Ex: this is the body "[1000]",it doesnt have any keys its a collection.
Thanks
Parth

dharmesh sharma

unread,
Sep 15, 2019, 12:41:11 AM9/15/19
to rest...@googlegroups.com
Dear,
You have to send converted list into json request don't send list in json request.

Thanks
Dharmesh sharma

--
You received this message because you are subscribed to the Google Groups "RestSharp" group.
To unsubscribe from this group and stop receiving emails from it, send an email to restsharp+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/restsharp/1a816f8c-bdbd-4598-b9ac-a7799cad91e2%40googlegroups.com.

Alexey Zimarev

unread,
Sep 15, 2019, 5:35:59 PM9/15/19
to RestSharp
I am not sure I follow your statement.

Have you looked at the code? All AddBody does is calling either AddJsonBody or AddXmlBody, nothing more

[Obsolete("Use AddXmlBody or AddJsonBody")]
public IRestRequest AddBody(object obj)
{
switch (RequestFormat)
{
case DataFormat.Json:
AddJsonBody(obj);
break;

case DataFormat.Xml:
AddXmlBody(obj);
break;
}

return this;
}

partha ss

unread,
Sep 16, 2019, 9:48:13 AM9/16/19
to RestSharp
This will be taken based on what i choose before making request.I have choosen json. The problem is my API is throwing exception if send a json object.if i send list<string> everything looks fine.

partha ss

unread,
Sep 16, 2019, 9:51:43 AM9/16/19
to RestSharp
If I dont send API is responding like you have sent null args.I have to choose jsonbodu as well as make request after selecting addBody command only
Ex: this is the collection that API accepts [121412]
If I say request.AddJsonBody(JsonConvert.SerializeObject(PostParam)); it will give response as null argument was passed and response is empty
If I say request.AddBody(PostParam); then everything works fine

Thanks


On Sunday, September 15, 2019 at 10:11:11 AM UTC+5:30, dharmesh sharma wrote:
Dear,
You have to send converted list into json request don't send list in json request.

Thanks
Dharmesh sharma

On Thu, Sep 12, 2019, 12:47 PM partha ss <part...@gmail.com> wrote:
I have to test a API which accepts List<String> val as body.If I use "request.addBody" everything works fine,But If i use addJsonbody it API throws exception that collection cant be null. S how to add a List<string> to body in request as alternative for addbody since this is deprecated.

Ex: this is the body "[1000]",it doesnt have any keys its a collection.
Thanks
Parth

--
You received this message because you are subscribed to the Google Groups "RestSharp" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rest...@googlegroups.com.

Alexey Zimarev

unread,
Sep 16, 2019, 10:43:09 AM9/16/19
to rest...@googlegroups.com
Maybe you can post your code, I don’t really understand the issue.

partha ss

unread,
Sep 17, 2019, 4:26:17 AM9/17/19
to RestSharp
Thanks for replay.This is the code
RestClient Client = new RestClient(ConfigManager.BaseUrl);
                RestRequest request = new RestRequest(endpoint, Method.POST);
                foreach (var headerVal in headers)
                {
                    request.AddHeader(headerVal.Key, headerVal.Value);
                }
                request.RequestFormat = DataFormat.Json;
                if (IsPayloadJson)
                    request.AddJsonBody(JsonConvert.SerializeObject(PostParam));//the input [12322] as list<string> returns null value from API,the api accepts List<string> as argument
                else
                    request.AddBody(PostParam);this works will if send here list<string>,but this is deprecated

Alexey Zimarev

unread,
Jan 6, 2020, 10:03:59 AM1/6/20
to RestSharp
Nothing really happened with AddBody. We just made it more explicit, you either need to call AddJsonBody or AddXmlBody. Everything else is the same.

If you have serialization issue, I'd suggest trying a different serializer.
Reply all
Reply to author
Forward
0 new messages