Google App Script external API return error 406

…衆ロ難τιáo~ 提交于 2019-12-20 06:26:30

问题


I'm trying to fetch a URL using optional advanced parameters in GAS:

function myFunction() {
  var options = {};
  options = {
    headers: {
      Authorization: 'Bearer ?????',
      'Content-Type': 'application/json',
    },
  };
  UrlFetchApp.fetch(<url>, options);
}

I always get:

Request failed for returned code 406 (line 52, file "Project").

HTTPResponse:

SyntaxError: Empty JSON string

I did the same fetching in Excel Power Query:

let
    Source = Json.Document(Web.Contents(<URL>, [Headers=[Authorization="Bearer 
?????????????", ContentType="application/json"]])),

And it works, it works also using Postman...

What's the problem with GAS? Please help me! Thanks in advance to everybody. Andrea


回答1:


406 error is because content negotiation between server and client has failed. While setting Content-Type, It's possible that the accepted return content is also advertised as 'application/json' by Power query and Postman, but UrlFetchApp doesn't do the same. Adding a explicit Accept header solves this:

Accept: 'application/json'

References:

  • 406 Not acceptable/Web MDN
  • RFC7231


来源:https://stackoverflow.com/questions/52625841/google-app-script-external-api-return-error-406

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!