gpt4 book ai didi

powershell - 休息 API : adapt a curl post to powershell

转载 作者:可可西里 更新时间:2023-11-01 16:40:33 25 4
gpt4 key购买 nike

我尝试使一个有效的 curl 命令适应 powershell

这是 postman 的工作 curl :

curl -X POST \
https://test.portal.com/api/v1/login \
-H 'authorization: Basic ADSFws343==' \
-H 'cache-control: no-cache' \
-H 'content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW' \
-H 'postman-token: 06944eab-3634-dd34-4634-0966bd8872c5' \
-F 'user[email]=xxx@xxx.com' \
-F 'user[password]=xxx'

这将创建以下原始数据

POST /api/v1/login HTTP/1.1
cache-control: no-cache
Postman-Token: 80b2b345-6f5d-4c63-bedf-c65038b1e5df
Authorization: Basic ADSFws343==
User-Agent: PostmanRuntime/3.0.11-hotfix.2
Accept: */*
Host: test.portal.com
accept-encoding: gzip, deflate
content-type: multipart/form-data; boundary=--------------------------266295771457397823506834
content-length: 309
Connection: close

----------------------------266295771457397823506834
Content-Disposition: form-data; name="user[email]"

xxx@xxx.com
----------------------------266295771457397823506834
Content-Disposition: form-data; name="user[password]"

xxx
----------------------------266295771457397823506834--

我得到以下响应:

HTTP/1.1 201 Created
Server: nginx/1.8.0
Date: Tue, 06 Jun 2017 13:42:54 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: close
Status: 201 Created
Cache-Control: max-age=0, private, must-revalidate
X-XSS-Protection: 1; mode=block
X-Request-Id: d1dd86e7-325a-43d5-bfda-46a9df4cc660
ETag: W/"3b793a3a1971dac89b48633e0e031237"
X-Frame-Options: SAMEORIGIN
X-Runtime: 0.115464
X-Content-Type-Options: nosniff
X-Powered-By: Phusion Passenger 5.1.4

在正文中包含预期的数据。


现在我努力使代码适应 powershell

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

$boundary = "--------------------------210183530616764504332035"
$URL = "https://test.portal.com/api/v1/login"
$email = "xxx@xxx.com"
$password= "xxx"

$headers = @{
"cache-control"="no-cache";
"Postman-Token"="3cf33ce8-716e-4ac1-b2da-c46df65d5307"
Authorization="Basic ADSFws343==";
"content-type"="multipart/form-data; boundary=$boundary"
"User-Agent"="PostmanRuntime/3.0.11-hotfix.2"
"Accept"="*/*"
"accept-encoding"="gzip, deflate"
}

$LF = "`n"
$bodyLines = (
"--$boundary",
"Content-Disposition: form-data; name=`"user[email]`"$LF",
$email,
"--$boundary",
"Content-Disposition: form-data; name=`"user[password]`"$LF",
$password,
"--$boundary--$LF"
) -join $LF

$response = Invoke-RestMethod -Uri $URL -Headers $headers -Method Post -TimeoutSec 20 -Body $bodyLines -DisableKeepAlive

这会引发异常:

Invoke-RestMethod : Incomplete response received from application

并创建以下原始数据

POST https://test.portal.com/api/v1/login HTTP/1.1
Content-Type: multipart/form-data; boundary=--------------------------210183530616764504332065
Accept: */*
Postman-Token: 3cf33ce8-716e-4ac1-b2da-c46df65d5307
Authorization: Basic ADSFws343==
accept-encoding: gzip, deflate
cache-control: no-cache
User-Agent: PostmanRuntime/3.0.11-hotfix.2
Host: test.portal.com
Content-Length: 300
Connection: Close

----------------------------210183530616764504332065
Content-Disposition: form-data; name="user[email]"

xxx@xxx.com
----------------------------210183530616764504332065
Content-Disposition: form-data; name="user[password]"

xxx
----------------------------210183530616764504332065--

和这个回应

HTTP/1.1 502 Bad Gateway
Server: nginx/1.8.0
Date: Tue, 06 Jun 2017 14:02:23 GMT
Content-Type: text/html; charset=UTF-8
Content-Length: 54
Connection: close
Status: 502 Bad Gateway
cache-control: no-cache, no-store, must-revalidate

<h2>Incomplete response received from application</h2>

我的错在哪里或缺少什么?原始数据的唯一区别是内容长度较短。甚至边界与 postman curl 的长度相同

感谢您抽出宝贵时间

编辑:将 Headers 更改为 hash-table 无效,并更改 powershell 代码以更准确地模拟 postman curl

最终解决方案:问题是正文格式错误。改变

$LF = "`n"

$LF = "`r`n"

解决了问题!

最佳答案

为您的 header 使用哈希表。有时您需要将它们包装在一个数组中

$headers = @{
authorization="Basic ADSFws343==";
"cache-control"="no-cache";
"content-type"="multipart/form-data";
boundary="----WebKitFormBoundary7MA4YWxkTrZu0gW";
"postman-token"="06944eab-3634-dd34-4634-0966bd8872c5"
}

$post =@{
email="xxx@xxx.com"
password="xxxxxxxx"
}

Invoke-WebRequest -Uri "https://test.portal.com/api/v1/login" -Method POST -Headers $headers -Body $Post -SessionVariable WebSession
$websession

关于powershell - 休息 API : adapt a curl post to powershell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44392734/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com