Why do I get an invalid content type when calling the create lead API?

Anonymous
Not applicable

Why do I get an invalid content type when calling the create lead API?

I can't seem to get the API to correctly return a successful response. The error I receive is Invalid Content Type. I've added 'application/json' the the content-type header but still I have no success. 
Tags (1)
5 REPLIES 5
SanfordWhiteman
Level 10 - Community Moderator

Re: Why do I get an invalid content type when calling the create lead API?

Please post your full code (maybe to a Gist or something like that because this forum isn't good for code blocks).
Anonymous
Not applicable

Re: Why do I get an invalid content type when calling the create lead API?

var client = new RestClient(req.endpoint);
            client.AddDefaultHeader("content-type", "application/json");
            // client.Authenticator = new HttpBasicAuthenticator(username, password);

            var request = new RestRequest("/v1/leads.json", Method.POST); 
            request.AddParameter("input", inbound.input); // adds to POST or URL querystring based on Method
            request.AddParameter("access_token", token.access_token); // adds to POST or URL querystring based on Method
            // easily add HTTP Headers
            request.AddHeader("content-type", "text/json");
             
            // execute the request
            IRestResponse response = client.Execute(request);
            var content = response.Content; // raw content as string

            // or automatically deserialize result
            // return content type is sniffed but can be explicitly set via RestClient.AddHandler();
            IRestResponse<ResponseUpdateCreateLead> response2 = client.Execute<ResponseUpdateCreateLead>(request);
Anonymous
Not applicable

Re: Why do I get an invalid content type when calling the create lead API?

You are defining two headers. Try removing this line in your code:
request.AddHeader("content-type", "text/json");

Also I am not sure if your code does this automatically, but I would make sure content-type is uppercase (Content-Type).
Anonymous
Not applicable

Re: Why do I get an invalid content type when calling the create lead API?

I had removed the first call to add the header and I made Content-Type case sensitive. However it is still not working. I continue to receive error 612.  Does anyone have a sample of working code to call a Create/UpdateLead ? Thanks in advance. 

            var client = new RestClient(req.endpoint);
            //client.AddDefaultHeader("content-type", "json"); 
            // client.Authenticator = new HttpBasicAuthenticator(username, password);

            var request = new RestRequest("/v1/leads.json", Method.POST); 
            request.AddParameter("action", "createOrUpdate");
            request.AddParameter("input", inbound.input); // adds to POST or URL querystring based on Method
            request.AddParameter("access_token", token.access_token);  
 
            // easily add HTTP Headers
            request.AddHeader("Content-Type", "text/json");
            request.AddHeader("accept", "text/json");
               
            // or automatically deserialize result
            // return content type is sniffed but can be explicitly set via RestClient.AddHandler();
            IRestResponse<ResponseUpdateCreateLead> response2 = client.Execute<ResponseUpdateCreateLead>(request);
            ResponseUpdateCreateLead res = response2.Data; 
Alt+MAlt+N
Anonymous
Not applicable

Re: Why do I get an invalid content type when calling the create lead API?

Hi Leonardo

Let's try
// request.AddHeader("content-type", "text/json");
request.AddHeader("content-type", "application/json");


http://developers.marketo.com/documentation/rest/error-codes/
Invalid Content Type
If you see this error, add a content type header specifying JSON format to your request. For example, try using "content type: application/json".