C# API Media type not supported - Error 1124

kBharathraj83
kBharathraj83 ✭✭
edited 12/09/19 in API & Developers

Hi Guys,

Trying API for first time. I am able to do GET sheet details successfully.

But when i post to create new row/column i am getting the following error "Invalid Content-Type header. Media type not supported" for the below code

var client = new RestClient("https://api.smartsheet.com/2.0/sheets/239613.../columns");

var request = new RestRequest(Method.POST);

request.AddHeader("Authorization", "Bearer zd......");

request.AddHeader("Content-Type", "application/json");

request.AddParameter("undefined", "{\"title\":\"New Date Columnyf\", \"type\":\"DATE\",                  \"validation\":\"true\", \"index\":4}", ParameterType.RequestBody);

IRestResponse response = client.Execute(request);

I am able to POST successfully  in Postman. Just C# code won't work.

I appreciate any help resolving this.

Thanks!

Comments

  • kBharathraj83
    kBharathraj83 ✭✭
    edited 11/26/18

    UPDATE:

    ISSUE RESOLVEDsmiley

    I was setting the JSON string directly as a parameter string to the request body. Which it did not like.

    Old code that did not work

    request.AddParameter("undefined", "{\"title\":\"New Date Columnyf\", \"type\":\"DATE\",                  \"validation\":\"true\", \"index\":4}", ParameterType.RequestBody);

    Fix

    If i build the JSON using Jsonserializer and then add it to the request it works. 

    Step 1: Create class 

    public class AddColumn

            {

                public string title { get; set; }

                public string type { get; set; }

                public string validation { get; set; }

                public int index { get; set; }

            }

     

    Step 2:  Use the class to create the object

    var objAddCol = new AddColumn

                {

                    title = "NewAPITestColumn1",

                    type = "DATE",

                    validation = "true",

                    index = 1

                };

    Step 3:  Pass the class object to JsonSerializer.Serialize and form the JSON

    var json = request.JsonSerializer.Serialize(objAddCol);

    Step 4:  use the JSON and add it to the request body

    request.AddParameter("application/json; charset=utf-8", json, ParameterType.RequestBody);

    Thanks!

  • IanN
    IanN Employee

    Hello kBharathraj83,

    Thanks for reaching out to us. I think we'll need to do a deeper dive on this. Please send an email to api@smartsheet.com and we will take a look as soon as we can. Please note that Thursday and Friday are holidays and there may be a slight delay in receiving a response, but we will get to it as soon as we can. 

    If you would, please also include the full text of the error message. A screenshot would also be helpful. 

    Thanks!

    Ian