对Jira REST API的JS查询工作正常,但与Jira Agile REST API的类似查询失败

时间:2019-09-21 11:34:52

标签: javascript rest jira jira-rest-api jira-agile

我正在尝试使用浏览器中的JavaScript通过Jira和Jira Agile REST API从Jira Cloud实例中获取数据。对Jira REST API的查询工作正常,但对Jira Agile REST API的查询相同,但响应失败

Response for preflight has invalid HTTP status code 401.

我正在使用具有用户ID和从Jira获得的API令牌的基本身份验证。使用cURL和ARC,我能够从Jira REST API和Jira Agile REST API中成功检索数据,因此针对这两个API的身份验证似乎都可以工作。在JS中,我尝试同时使用fetch()和jquery ajax(),结果是相同的。

function fetchFromJira(url, id, token) {
  const authorizationString = 'Basic ' + btoa(id + ':' + token);
  const options = {
    method: 'GET',
    headers: {
      Authorization: authorizationString,
      'Content-Type': 'application/json',
    },
  };

  fetch(url, options)
    .then(response => {
      if (response.ok) {
        return response.json();
      } else {
        throw new Error(response.status);
      }
    })
    .then(json => {
      console.log(json);
    })
    .catch(error => {
      console.log(error);
    });
}

fetchFromJira(
  'https://fredrikastrom.atlassian.net/rest/api/latest/issue/10000',
  '<user id>',
  '<API token>'
); // successful

fetchFromJira(
  'https://fredrikastrom.atlassian.net/rest/agile/1.0/board',
  '<user id>',
  '<API token>'
); // fails

控制台上的输出如下:

test.js:11 OPTIONS https://fredrikastrom.atlassian.net/rest/agile/1.0/board 401 ()  
fetchFromJira @ test.js:11
(anonymous) @   test.js:33
index.html:1 Failed to load https://fredrikastrom.atlassian.net/rest/agile/1.0/board: Response for preflight has invalid HTTP status code 401.
test.js:23 TypeError: Failed to fetch
test.js:20 {expand: "renderedFields,names,schema,operations,editmeta,changelog,versionedRepresentations", id: "10000", self: "https://fredrikastrom.atlassian.net/rest/api/latest/issue/10000", key: "FAT-1", fields: {…}}

以下是对Jira REST API成功查询的预检请求和响应标头:

t=3241 [st= 89]        HTTP_TRANSACTION_HTTP2_SEND_REQUEST_HEADERS
                       --> :authority: fredrikastrom.atlassian.net
                           :method: OPTIONS
                           :path: /rest/api/latest/issue/10000
                           :scheme: https
                           accept: */*
                           accept-encoding: gzip, deflate, br
                           accept-language: sv-SE,sv;q=0.9,en-US;q=0.8,en;q=0.7,fi;q=0.6
                           access-control-request-headers: authorization,content-type
                           access-control-request-method: GET
                           cache-control: no-cache
                           origin: http://127.0.0.1:8080
                           pragma: no-cache
                           referer: http://127.0.0.1:8080/test/index.html
                           user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.62 Safari/537.36
t=3241 [st= 89]     -HTTP_TRANSACTION_SEND_REQUEST
t=3241 [st= 89]     +HTTP_TRANSACTION_READ_HEADERS  [dt=68]
t=3278 [st=126]        HTTP2_STREAM_UPDATE_SEND_WINDOW
                       --> delta = 0
                       --> stream_id = 1
                       --> window_size = 65535
t=3309 [st=157]        HTTP_TRANSACTION_READ_RESPONSE_HEADERS
                       --> HTTP/1.1 200
                           status: 200
                           server: AtlassianProxy/1.15.8.1
                           vary: Accept-Encoding
                           cache-control: no-cache, no-store, no-transform
                           content-type: text/html;charset=UTF-8
                           content-encoding: gzip
                           strict-transport-security: max-age=315360000; includeSubDomains; preload
                           date: Sat, 12 Oct 2019 06:33:50 GMT
                           atl-traceid: 519aa518a8e8e5ea
                           x-arequestid: c68d7b95-3635-49e1-a2fd-971e0502adf5
                           x-xss-protection: 1; mode=block
                           timing-allow-origin: *
                           x-content-type-options: nosniff
                           set-cookie: atlassian.xsrf.token=7a27221d-39bc-4555-9569-b26a0beb9689_b9e038120f5696c0bac7202f986ee24d3752c6fa_lout; Path=/; Secure

这是失败的请求到Jira Agile REST API的相应标头:

t=5918 [st= 5]        HTTP_TRANSACTION_HTTP2_SEND_REQUEST_HEADERS
                      --> :authority: fredrikastrom.atlassian.net
                          :method: OPTIONS
                          :path: /rest/agile/latest/board
                          :scheme: https
                          accept: */*
                          accept-encoding: gzip, deflate, br
                          accept-language: en-GB,en-US;q=0.9,en;q=0.8
                          access-control-request-headers: authorization,content-type
                          access-control-request-method: GET
                          origin: http://127.0.0.1:8080
                          referer: http://127.0.0.1:8080/test/index.html
                          user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.62 Safari/537.36
t=5919 [st= 6]     -HTTP_TRANSACTION_SEND_REQUEST
t=5919 [st= 6]     +HTTP_TRANSACTION_READ_HEADERS  [dt=65]
t=5984 [st=71]        HTTP_TRANSACTION_READ_RESPONSE_HEADERS
                      --> HTTP/1.1 401
                          status: 401
                          server: AtlassianProxy/1.15.8.1
                          vary: Accept
                          www-authenticate: OAuth realm="https%3A%2F%2Ffredrikastrom.atlassian.net"
                          cache-control: no-transform
                          content-type: application/xml;charset=UTF-8
                          strict-transport-security: max-age=315360000; includeSubDomains; preload
                          date: Sat, 12 Oct 2019 07:05:10 GMT
                          atl-traceid: 2caf28fb1cce9a77
                          x-arequestid: 817e2b89-e3d1-431b-b892-781fc78c9669
                          x-xss-protection: 1; mode=block
                          timing-allow-origin: *
                          x-content-type-options: nosniff
                          set-cookie: atlassian.xsrf.token=7a27221d-39bc-4555-9569-b26a0beb9689_dafc86c05dbdc472c9b99300b351fe0dd62b305d_lout; Path=/; Secure
                          content-length: 174

有趣的是,即使请求使用相同的功能(仅请求的URL有所不同)发出,请求标头的外观也略有不同。后续请求包括缓存控制标头和编译指示标头,接受语言标头包括另一种语言。但是这些都不会对服务器是否接受预检请求有合理的影响?

任何线索为何一个请求成功而另一个请求失败?

0 个答案:

没有答案
相关问题