【问题标题】:Getting 400 Bad Request Errors using Invoke-RestMethod for Microsoft Cognitive API使用 Microsoft Cognitive API 的 Invoke-RestMethod 获取 400 个错误请求错误
【发布时间】:2016-07-03 02:50:21
【问题描述】:

我们正在尝试测试和评估来自 Microsoft 认知服务的文本分析 API。我们正在尝试使用 Invoke-RestMethod 让快速而肮脏的 PowerShell 脚本正常工作。经过一些调整后,我们仍然收到 400 个错误返回给我们。我们不确定出了什么问题,因为 JSON 似乎已更正,而且我们输入的 API 密钥似乎是正确的。我们利用了我们在另一个人的博客上找到的关于使用附加标题的内容,并尝试了一些变体,但仍然没有骰子。有人可以为我们做一次健全性检查吗?

#html tag stripper function
function htmlStrip ($results)
    {
    #using .NET toString method to ensure PS doesn't interpret same var incorrectly
    $results = $results.toString()
    $results -replace '<[^>]*(>|$)'
    }


Try
{


    [string]$sourceUrl = Read-Host "Enter a URL such as https://foobar.com"
}
Catch
{
    Write-Host "URL requires http:// or https:// prefix e.g. https://cnn.com"
}


$webClient = New-Object Net.WebClient
[string]$results = $webClient.DownloadString($sourceUrl)


[string]$cleanResults = htmlStrip $results


$body = 
[ordered]@{"documents" = 
    @{ "language" = "en"; "id" = $sourceUrl; "text" = $cleanResults }
    }

#>

$body = [ordered]@{
    "documents" = 
    @(
        @{ "language" = "en"; "id" = $sourceUrl; "text" = $cleanResults }
    )
}

$jsonBody = $body | ConvertTo-Json

#Begin Text Analytics API Call with Invoke-RestMethod wrapper
#[string]$apiUrl = "https://westus.api.cognitive.microsoft.com/text/analytics/v2.0/keyPhrases"
[string]$apiKey = "REDACTED"


$headers = @{ "Ocp-Apim-Subscription-Key" = $apiKey }

$analyticsResults = Invoke-RestMethod -Method Post -Uri $apiUrl -Headers $headers -Body $jsonBody -ContentType "application/json"  -ErrorAction Stop

Write-Host $analyticsResults

Write-Host $jsonBody

【问题讨论】:

    标签: powershell microsoft-cognitive


    【解决方案1】:

    您放入请求的text 属性中的数据可能无效。
    我用 GitHub 上 TypeScript 存储库的 README.md 的修复 URL 尝试了你的脚本,它可以工作。

    您的脚本(略微缩短)

    $sourceUrl = 'https://raw.githubusercontent.com/Microsoft/TypeScript/master/README.md'
    
    $webClient = New-Object Net.WebClient
    $results = $webClient.DownloadString($sourceUrl)
    
    $body = [ordered]@{
        "documents" = 
        @(
            @{ "language" = "en"; "id" = $sourceUrl; "text" = $results }
        )
    }
    
    $jsonBody = $body | ConvertTo-Json
    
    $apiUrl = "https://westus.api.cognitive.microsoft.com/text/analytics/v2.0/keyPhrases"
    $apiKey = "..."
    
    $headers = @{ "Ocp-Apim-Subscription-Key" = $apiKey }
    
    $analyticsResults = Invoke-RestMethod -Method Post -Uri $apiUrl -Headers $headers -Body $jsonBody -ContentType "application/json"  -ErrorAction Stop
    $analyticsResults.documents.keyPhrases
    

    结果

    TypeScript compiler
    gulp tests
    g typescript
    TypeScript source
    built compiler
    TypeScript users
    g gulp
    TypeScript directory
    cd TypeScript
    gulp baseline
    gulp lint
    gulp local
    gulp clean
    gulp runtests-browser
    gulp LKG
    Install Gulp tools
    ...
    

    【讨论】:

    • 我明白了,谢谢分享!也许微软正在禁止某些类型的可打印或不可打印字符。可能是反 sql 注入类型的字符。非常感谢您为我们验证!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-27
    • 2013-06-30
    • 2020-02-14
    • 1970-01-01
    • 2018-11-20
    • 2022-01-04
    相关资源
    最近更新 更多