首页 > 解决方案 > 无法查询 JavaScript REST API

问题描述

我正在编写 PowerShell 脚本来列出页面https://testpage.com/api/usergroups中的所有信息。

怎样才能得到输出?

为此,我使用了下一个代码

function Get-Data([string]$username, [string]$password, [string]$url) {
    # Step 1. Create a username:password pair
    $credPair = "$($username):$($password)"

    # Step 2. Encode the pair to Base64 string
    $encodedCredentials =[System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($credPair))

    # Step 3. Form the header and add the Authorization attribute to it
    $headers = @{ Authorization = "Basic $encodedCredentials" }

    # Step 4. Make the GET request
    $responseData = Invoke-WebRequest -Uri $url -Method Get -Headers $headers -UseBasicParsing

    return $responseData
}
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

Get-Data -username user -password pass -url https://testpage.com/api/usergroups

结果我得到了下一个错误

<noscript>
    <p>
        <strong>Note:</strong> Since your browser does not support JavaScript,
        you must press the Continue button once to proceed.
    </p>
</noscript>

标签: restapipowershellscripting

解决方案


推荐阅读