'How to create custom field definitions with Sendgrid (v3) in C#?

According to the V3 Sendgrid documentation, I can create custom definitions using their API: https://docs.sendgrid.com/api-reference/custom-fields/create-custom-field-definition

My code is as follows:

        public static async void addCustomDefinition(SendGridClient client)
        {
                var data = "{ 'name': 'test_name', 'field_type': 'Text' }";

                var response = await client.RequestAsync(
                    method: SendGridClient.Method.POST,
                    urlPath: "marketing/field_definitions", //"contactdb /custom_fields",
                    requestBody: data
                );

                Console.WriteLine(response.StatusCode);
                Console.WriteLine(response.Body.ReadAsStringAsync().Result);
                Console.WriteLine(response.Headers.ToString());

        }

However, I got the following output error message:

BadRequest {"errors":[{"field":"body","message":"invalid character '\'' looking for beginning of object key string"}]}

What am I doing incorrectly?

PS: I changed the urlPath parameter:

...from online guide: urlPath: "v3/marketing/field_definitions,

to this version: urlPath: "marketing/field_definitions",

this newer version works for me on other commands such as getting field definitions and creating contact list



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source