Sending an array of strings with restsharp

69 Views Asked by At

I am trying to include a tag when I am calling the Freshdesk outbound_email API, in the API docs it says the type for tags is an array of strings, so I want to send something like ["tag1", "tag2", tag3"] but I am not sure how to add it as a parameter with restsharp.

I have tried doing request.AddParameter but it won't accept an array as a parameter.

I have also tried doing JSON convert and then adding it as a parameter but the end point doesn't accept it.

1

There are 1 best solutions below

0
YouShallNotPass On

This was confusing for me at first as well, since the Freshdesk API says that tags are a string array. It appears that the tags[] parameter only works when sending strings, and not arrays or lists (when sending from RestSharp at least)!

The incorrect way to set tags, which results in the tag showing up as a System.String[] in Freshdesk:

List<string> tags = new() { "tag1", "tag2" };
request.AddParameter("tags[]", tags.ToArray());

The correct way to set tags, which adds a parameter per tag (you can also just reuse the AddParameter line for however many you need):

List<string> tags = new() { "tag1", "tag2" };
foreach (var tag in tags)
{
    request.AddParameter("tags[]", tag);
}