You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将获取X-SDS-AUTH-TOKEN的Curl命令转为PowerShell脚本?

Convert Curl Auth Request to PowerShell Script

Here's a direct translation of your curl command into PowerShell, along with steps to extract the X-SDS-AUTH-TOKEN:

Basic Script (PowerShell 6+)

# Replace with your actual credentials
$username = "username"
$password = "password"

# Encode credentials for Basic Authentication (equivalent to curl's -u flag)
$authBase64 = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("$username`:$password"))

# Define request headers
$headers = @{ "Authorization" = "Basic $authBase64" }

try {
    # Send request, mirroring curl's -L (follow redirects), -k (ignore SSL), -v (verbose)
    $response = Invoke-WebRequest -Uri "https://172.31.110.16:4443/login" `
        -Headers $headers `
        -SkipCertificateCheck `
        -MaximumRedirection 10 `
        -Verbose

    # Extract the auth token from response headers
    $authToken = $response.Headers["X-SDS-AUTH-TOKEN"]
    if ($authToken) {
        Write-Host "X-SDS-AUTH-TOKEN: $authToken"
    } else {
        Write-Host "Token not found in response headers."
    }
}
catch {
    Write-Error "Request failed: $_"
}

For Windows PowerShell 5.1

If you're using the older Windows PowerShell 5.1, the -SkipCertificateCheck switch isn't available. Add this code at the start of your script to bypass SSL certificate validation (equivalent to curl's -k):

# Bypass SSL certificate validation (only for Windows PowerShell 5.1)
Add-Type @"
using System.Net;
using System.Security.Cryptography.X509Certificates;
public class TrustAllCertsPolicy : ICertificatePolicy {
    public bool CheckValidationResult(
        ServicePoint srvPoint, X509Certificate certificate,
        WebRequest request, int certificateProblem) {
        return true;
    }
}
"@
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy

Key Equivalents to Your Curl Command

  • $authBase64 construction: Replaces curl's -u username:password (Basic Authentication header)
  • -SkipCertificateCheck: Replaces curl's -k (disables SSL certificate verification)
  • -MaximumRedirection 10: Replaces curl's -L (follows up to 10 redirects; adjust the number if needed)
  • -Verbose: Provides detailed request/response logs like curl's -v

Troubleshooting Tips

  • If the token isn't returned, verify if the login endpoint expects a POST request instead of GET. If so, add the -Method POST parameter to Invoke-WebRequest.
  • Check if the service requires additional headers (e.g., Content-Type). You can add these to the $headers hashtable.
  • Inspect the full response content with $response.Content to see if the token is embedded in the body instead of headers.

内容的提问来源于stack exchange,提问作者j-onathan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 11:21:07