I try to adapt a working curl command to powershell
this is the working curl from postman:
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'
This creates the following raw data
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--
and I get the following response:
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
with the expected data in the body.
Now I struggle to adapt the code to 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
This throws an exception:
Invoke-RestMethod : Incomplete response received from application
and creates the following raw data
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--
and this response
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>
Where is my fault or what is missing? The only difference in the Raw Data is the somehow shorter content-length. even the boundarys have the same length as the postman curl
Thank you for taking your time
Edit: Changed the Headers to hash-table without effect and changed the powershell code to simulate the postman curl more accurate
Final Solution: Problem was the wrong format of the body. Changing
$LF = "`n"
to
$LF = "`r`n"
fixed the problem!