我努力去適應工作curl命令到PowerShell的REST API:適應捲曲後到PowerShell的
這是從郵遞員的工作捲曲:
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][email protected]' \
-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]"
[email protected]
----------------------------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 = "[email protected]"
$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]"
[email protected]
----------------------------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>
哪裏是我的錯還是缺什麼?原始數據的唯一區別在於內容長度較短。即使是boundarys具有相同的長度爲郵差捲曲
感謝您抽出時間
編輯:改變了頭哈希表沒有影響,改變了PowerShell代碼來模擬郵遞員捲曲更準確
最終解決方案:問題是身體的錯誤格式。更改
$LF = "`n"
到
$LF = "`r`n"
固定的問題!
你控制服務器?你有沒有做過任何分析呢?您可以嘗試更改用戶代理。另外,如果你使用'Invoke-WebRequest'而不是'Invoke-RestMethod',你會得到更多關於服務器返回的內容(代碼,消息,頭文件)的數據。 – briantist
「Get-Help Invoke-WebRequest -Full」和Invoke-RestMethod ocourse –
作爲開始,你可能想嘗試一下沿着$ headers = @ {'header1 name'='header1 value';'header2 name'= 'header2 value'}你也可能(或可能不)想用「這裏的字符串」作爲「body」部分 –