如何从REST API下载JSON数据集



我需要访问REST API并下载JSON数据集。文档说明了如何在Python中做到这一点:

import requests
data={
'sso_operation': 'authenticate_user',
'user': 'joe.blogs@gmial.com',
'password': 'this_1s_Private'
}
r1 = requests.post('WEBSSO_URL', data=data, verify=False, allow_redirects=True)
r2 = requests.get('TARGET URL', cookies=r1.cookies, verify=False, allow_redirects=True)
print (r2.status_code)
print(r2.text)

它看起来像两步后得到。我唯一能想到的是:

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$user = "xxxx.com"
$password = "xxxx"
$restURL = "xxx"
$headers = @{Authorization = 'Basic ' + 
[Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("${user}@${account}:${password}"))}
$response = Invoke-WebRequest -Uri $restURL -Headers $headers
$response.content

有人能帮我做这两步帖子吗?

由于我们不知道您所指的文档,我将以这种方式从Post方法开始。我把data变量放在了头中,因为我通常会通过头进行身份验证,并单独发送一个JSON主体。如果我弄错了,请删除标题并将所有内容放入$body。如果结果是一个身份验证密钥,它将存储在$postResult中,您可以重用它。

$header = @{
'sso_operation' = 'authenticate_user'
'user' = 'your.user'
'password' = 'your.password'
}
$body = @{
'JSON' = 'body'
} | ConvertTo-Json
$postResult = Invoke-RestMethod -Uri $url -Headers $header -Body $body -ContentType 'application/json'

如果您使用JSON格式进行交互,我建议使用Invoke-RestMethod。我现在假设"Post"调用的响应返回了我放置在标头中的密钥。

$header = @{
'Authentication' = $postResult
}
$getResult = Invoke-RestMethod -Uri $url -Headers $header

最新更新