Getting a 415 error (Google Apps Scripts)

548 views
Skip to first unread message

Roger Marsh

unread,
Mar 9, 2015, 5:34:52 AM3/9/15
to capsu...@googlegroups.com
Hi All,

I am trying to create opportunities using the following script but am getting 415 errors, "message":"Missing or invalid Content-Type header". I have checked the headers and they look good! Any ideas where I'm going wrong?

function myCapsule() {

var url = 'https://example.capsulecrm.com/api/party/111111111/opportunity';

var data =
{
"opportunity": {
"name": "From Google Apps",
"partyId": "1111111111",
"milestone": "Milestone"
}
};

var payload = JSON.stringify(data);

var headers = {
'Content-type': 'application/json',
'Authorization': 'Basic '+ Utilities.base64Encode('apikey:a-letter')
};


var options =
{
'headers' : headers,
'method' : 'post',
'payload' : payload
};

var capsulePost = UrlFetchApp.fetch(url, options);

Michael Josephson

unread,
Mar 9, 2015, 5:54:45 AM3/9/15
to capsu...@googlegroups.com
Hi Roger!

It looks like your code is pretty close. There is a setting in the fetch options for setting the content type which has precedence over a content type you try to set by including a Content-Type header in headers yourself. The default value for the contentType option is application/x-www-form-urlencoded so you need to override this in your options like so:

var options = {
    'contentType' : 'application/json',
    'headers' : headers,
    'method' : 'post',
    'payload' : payload
};

To get JSON back in your response as well you should add 'Accept' to the headers:

var headers = {    
    'Accept': 'application/json',
    'Authorization': 'Basic '+ Utilities.base64Encode('apikey:a-letter')
};

adding Accept is a separate point though, it's putting the content type in options that's key to fixing the 415.

Give those changes a try and your request should be accepted now.

Cheers,

-Michael

Roger Marsh

unread,
Mar 9, 2015, 8:08:45 AM3/9/15
to capsu...@googlegroups.com
Perfect! That did the trick.

Best,
Roger

Thanks so much for your help.

Capsule Support

unread,
Mar 9, 2015, 8:41:48 AM3/9/15
to Roger Marsh, capsu...@googlegroups.com
Glad you're all sorted Roger. Will be sure to pass on your thanks to Michael

Have a super week, all the best

--
Rachel
Capsule Support
sup...@capsulecrm.com


How would you rate my reply?
Great   Okay   Not Good


--
You received this message because you are subscribed to the Google Groups "Capsule API" group.
To unsubscribe from this group and stop receiving emails from it, send an email to capsule_api...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



On Mon, Mar 9, 2015 at 9:35:08 GMT, Roger Marsh <ro...@plastarc.com> wrote:
Hi All,

I am trying to create opportunities using the following script but am getting 415 errors, "message":"Missing or invalid Content-Type header". I have checked the headers and they look good! Any ideas where I'm going wrong?

function myCapsule() {

  var url = 'https://example.capsulecrm.com/api/party/111111111/opportunity';

  var data =
  {
    "opportunity": {
      "name": "From Google Apps",
      "partyId": "1111111111",
      "milestone": "Milestone"
    }
  };

  var payload = JSON.stringify(data);

   var headers = {
     'Content-type': 'application/json',

     'Authorization': 'Basic '+ Utilities.base64Encode('apikey:a-letter')
    };


  var options =
    {

      'headers' : headers,
      'method' : 'post',
      'payload' : payload
};

  var capsulePost = UrlFetchApp.fetch(url, options);

--
You received this message because you are subscribed to the Google Groups "Capsule API" group.
To unsubscribe from this group and stop receiving emails from it, send an email to capsule_api...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



{#HS:76523916-102835#}
Reply all
Reply to author
Forward
0 new messages