Home > Software engineering >  C# converting httpclient post into restsharp post without async method on autopilot
C# converting httpclient post into restsharp post without async method on autopilot

Time:09-16

I have this piece of code that I'm trying to convert from async task [Http client post] into a rest sharp post method. Here is the working example of async task method that works with httpclient:

 static async Task addEmailToListAsync(string ListName, string Email, string FirstName, string LastName)
    {
        try
        {
            var baseAddress = new Uri("https://api2.autopilothq.com/");

            using (var httpClient = new HttpClient { BaseAddress = baseAddress })
            {

                httpClient.DefaultRequestHeaders.TryAddWithoutValidation("autopilotapikey", "mykey");

                using (var content = new StringContent(""))
                {
                    using (var response = await httpClient.PostAsync("v1/list/"   ListName   "/contact/"   Email, content))
                    {
                        string responseData = await response.Content.ReadAsStringAsync();
                    }
                }
            }
        }
        catch (Exception)
        {

            await addEmailToListAsync(ListName, Email, FirstName, LastName);
        }


    }

This method works and autopilot returns a response in form of " { } " - which indicated everything went well.

Now I'm trying to convert this using restsharp library and to achieve so that the method is not async task - but a simple void function:

 public static void addEmailToList(string ListName, string Email, string FirstName, string LastName)
        {

            var client = new RestClient("https://api2.autopilothq.com/");

            
            client.AddDefaultHeader("autopilotapikey", "mykey");

            var request = new RestRequest("v1/list",Method.POST);

            var jsonObj = JsonSerializer.Serialize(new 
            { 
                ListName = ListName,
                Contact = Email
            });

            request.AddParameter("application/json", jsonObj, ParameterType.RequestBody);

            var res = client.Execute(request).Content;

            var test = "";
        }

This is the example that I tried when converting - but I keep getting message with restsharp that body data is invalid...

What am I doing wrong here? I'm pulling my hairs out and I can't figure it out..

Can someone help me out?

CodePudding user response:

Your working code sends the list name and email address in the URL, and an empty string as the request body.

Your non-working version sends the list name and email address as a JSON-serialized string in the body, and omits them from the URL.

The error message suggests that the API you're calling doesn't support the second method of passing data. Change your RestSharp code to match the URL and request body from your HttpClient code.

var request = new RestRequest("v1/list/"   ListName   "/contact/"   Email, Method.POST);
var res = client.Execute(request).Content;
  • Related