C# API Media type not supported - Error 1124
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
-
UPDATE:
ISSUE RESOLVED
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!
-
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
Categories
- All Categories
- 14 Welcome to the Community
- Smartsheet Customer Resources
- 64.1K Get Help
- 414 Global Discussions
- 221 Industry Talk
- 461 Announcements
- 4.8K Ideas & Feature Requests
- 143 Brandfolder
- 141 Just for fun
- 58 Community Job Board
- 462 Show & Tell
- 32 Member Spotlight
- 1 SmartStories
- 299 Events
- 38 Webinars
- 7.3K Forum Archives