2017-06-06 62 views
2

我努力去適應工作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" 

固定的問題!

+0

你控制服務器?你有沒有做過任何分析呢?您可以嘗試更改用戶代理。另外,如果你使用'Invoke-WebRequest'而不是'Invoke-RestMethod',你會得到更多關於服務器返回的內容(代碼,消息,頭文件)的數據。 – briantist

+0

「Get-Help Invoke-WebRequest -Full」和Invoke-RestMethod ocourse –

+0

作爲開始,你可能想嘗試一下沿着$ headers = @ {'header1 name'='header1 value';'header2 name'= 'header2 value'}你也可能(或可能不)想用「這裏的字符串」作爲「body」部分 –

回答

0

multipart/formdata的主體格式錯誤,需要是CR LF而不是LF。

變化

$LF = "`n" 

$LF = "`r`n" 

,使其工作

0

使用哈希表爲您的標題。有時候,你需要用他們陣中

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

$post [email protected]{ 
    email="[email protected]" 
    password="xxxxxxxx" 
} 

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

中發送的原始數據, 「boundary = ---- WebKitFormBoundary7MA4YWxkTrZu0gW」困擾我;) –

+0

哈哈很好找:) :) – ArcSet

+0

修正了標題的建議,響應依然如此。編輯原始文章以反映更改並使讀者更易於比較。 由於缺少cmdlet的multipart/form-data支持,$ post數據不能被簡化。 – burglar

0

嘗試像

$user = 'user' 
$password= 'passwd' 

$params = @{ 
    Uri = 'https://httpbin.org/basic-auth/user/passwd' 
    Method = 'Get' 
    Headers = @{ 
     Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("$($user):$($password)")) 
    } 
} 

Invoke-RestMethod @params 

讓我困擾的是,你似乎可以用兩個HTTP基本驗證以及用戶名/密碼在身,提交形成。也許類似

$user = 'user' 
$password= 'passwd' 

$boundary = '----------------------------266295771457397823506834' 

$body = @" 
$boundary 
Content-Disposition: form-data; name="user[email]" 

$user 
$boundary 
Content-Disposition: form-data; name="user[password]" 

$password 
$boundary-- 
"@ 

$params = @{ 
    Uri = 'https://httpbin.org/basic-auth/user/passwd' 
    Method = 'Post' 
    Headers = @{ 
     Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("$($user):$($password)")) 
     'Content-Type' = "multipart/form-data; boundary=$boundary" 
    } 
    Body = $body 
} 

Invoke-RestMethod @params 
+0

標題中的授權正常工作,因爲它似乎。如果我放棄或弄亂它,我會得到未經授權的回覆。但它只是爲了連接。正文中的字段是用於訪問API背後的憑證,我不得不將它作爲多部分/表單數據發送。作爲迴應,我得到一個令牌,以便與API進一步溝通。謝謝 – burglar

+0

不確定,但你也可能在「邊界」中出現錯誤的「----」號或者其他一些愚蠢的事情。 Id通常會嗅探流量並嘗試在PowerShell中完全複製它。重定向,餅乾/會話變量等 –

+0

感謝你的努力,猜測沒有你指向我的邊界我不會發現問題!儘管邊界不是問題,但它非常接近:由於multipart/formdata的rfc,正確的換行符不僅僅是LF,而且是CR LF。因此,在將$ LF =「'n」更改爲$ LF =「'r'n」後,它就像一個魅力! – burglar