2016-11-03 73 views
0

我正在嘗試爲他們的API端點List Users實現Okta的分頁。它看起來像爲了分頁,必須通過其響應中的傳入標題獲得下一鏈接。當通過命令行的cUrl或Postman執行它們的List Users API端點時,標題中的所有內容看起來都很棒,但問題是使用cUrl或guzzle從PHP腳本運行它時,鏈接 html標籤將從標題中剝離爲如下圖所示:如何在分頁時從Okta API列表用戶的響應標題中獲取下一個鏈接?

HTTP/1.1 200 OK 
Content-Type: application/json 
Link: <https://your-domain.okta.com/api/v1/users?limit=200>; rel="self" 
Link: <https://your-domain.okta.com/api/v1/users? after=00ud4tVDDXYVKPXKVLCO&limit=200>; rel="next" 

我搜索了一會兒,並不能找到一個解決方案:爲

HTTP/1.1 200 OK 
Date: Thu, 03 Nov 2016 19:36:34 GMT 
Server: nginx 
Content-Type: application/json;charset=UTF-8 
Vary: Accept-Encoding 
X-Okta-Request-Id: WBuTwqhxlYz3iu5PY1jqHQZZBMU 
X-Rate-Limit-Limit: 1200 
X-Rate-Limit-Remaining: 1198 
X-Rate-Limit-Reset: 1478201841 
Cache-Control: no-cache, no-store 
Pragma: no-cache 
Expires: 0 
Link: ; rel="self" 
Strict-Transport-Security: max-age=315360000 

標題應該改爲外觀。有沒有人遇到過這個問題?提前致謝。

回答

2

請確保您的請求中包含Accept: application/json標題。我的猜測是PHP的cURL或Guzzle使用了不同的MIME類型,如text/plain

我能夠重現您的問題有以下curl命令,它沒有給出結果:

curl --verbose \ 
     --header "Authorization: SSWS ${API_KEY}" \ 
     --header "Content-Type: application/json" \ 
     --header "Accept: text/plain" \ 
     "https://${ORG}/api/v1/users?limit=1" 2>&1 | grep 'Link: ' 

但是,如果我改變Accept:application/json我得到了Link:頭:

curl --verbose \ 
     --header "Authorization: SSWS ${API_KEY}" \ 
     --header "Content-Type: application/json" \ 
     --header "Accept: application/json" \ 
     "https://${ORG}/api/v1/users?limit=1" 2>&1 | grep 'Link: ' 

< Link: <https://example.okta.com/api/v1/users?limit=1>; rel="self" 
< Link: <https://example.okta.com/api/v1/users?after=012a3b456cdefgHijK7l8&limit=1>; rel="next" 
+0

謝謝你的回覆。因此,看起來就像我的情況,我們使用php的print_r()打印標題,由於某種原因,該功能從標題中去除鏈接的內容。不過,你的頭文件中的MIME類型是正確的。再次感謝! – user1370897

0

我在搜索別的東西時發現了你的文章,我最近解決了這個問題,這是我的解決方案。所有這些都是用PowerShell編寫的。

#Function to automatically get all listings by pagination, this function will use the default Okta Limit parameter. Which is 1000 as the time of this making. 
#Invoke-OktaPagedMethod is based on the _oktaRecGet() function from https://github.com/mbegan/Okta-PSModule/blob/master/Okta.psm1 
function Invoke-OktaPagedMethod { 
    param 
    (
     [string]$Uri, 
     [array]$col, 
     [int]$loopcount = 0 
    ) 

    try { 
     #[System.Net.HttpWebResponse]$response = $request.GetResponse() 
     $OktaResponse = Invoke-WebRequest -Method Get -UseBasicParsing -Uri $Uri -Headers $OktaHeaders -TimeoutSec 300 

     #Build an Hashtable to store the links 
     $link = @{} 
     if ($OktaResponse.Headers.Link) { # Some searches (eg List Users with Search) do not support pagination. 
      foreach ($header in $OktaResponse.Headers.Link.split(",")) { 
       if ($header -match '<(.*)>; rel="(.*)"') { 
        $link[$matches[2]] = $matches[1] 
       } 
      } 
     } 

     $link = @{ 
      next = $link.next 
     } 

     try { 
      $psobj = ConvertFrom-Json -InputObject $OktaResponse.Content 
      $col = $col + $psobj 
     } catch { 
      throw "Json Exception : " + $OktaResponse 
     } 
    } catch { 
     throw $_ 
    } 

    if ($link.next) { 
     $loopcount++ 
     if ($oktaVerbose) { Write-Host "fetching next page $loopcount : " -ForegroundColor Cyan} 
     Invoke-OktaPagedMethod -Uri $link.next -col $col -loopcount $loopcount 
    } else { 
     return $col 
    } 
} 
相關問題