'Issue sending in method to UrlFetchApp.fetch in GAS

Stupid question here, but I am pulling my hair out (i'm bald) as to what I could possibly be doing wrong. I am doing a UrlFetchApp.fetch in google app scripts:

var result = UrlFetchApp.fetch(url, { headers: headers });

When using this as my headers, it works:

  var headers = {
    Authorization: 'Bearer ' + getAccessToken()
  }

Now I need to pass in 'method' as 'PUT' instead of the default of 'GET' (above), and and trying like this:

  var headers = {
      method : 'put',
      headers : {
        Authorization: 'Bearer ' + getAccessToken()
      }
  }

When I try sending this, it is not finding the token:

Exception: Request failed for https://api.spotify.com returned code 401. Truncated server response: {
  "error": {
    "status": 401,
    "message": "No token provided"
  }

How can I format the headers so that I can pass in the Authorization with token and also pass in 'method' as 'PUT'?

I see in the URLFetchApp.Fetch documentation for Google, that this is passed into options:

https://developers.google.com/apps-script/reference/url-fetch/url-fetch-app

headers Object  a JavaScript key/value map of HTTP headers for the request
method  String  the HTTP method for the request: get, delete, patch, post, or put. The default is get.

Thank you so much for any help!

phi



Sources

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

Source: Stack Overflow

Solution Source