Using Curl & Powershell to POST Form Data to CakePHP

Written by James McDonald

September 30, 2021

CakePHP 2 Form and trying to POST to it using curl

This is what I am trying to duplicate with curl

Content-Disposition: form-data; name="_method"

Content-Disposition: form-data; name="data[PrintLabel][printer]"

Content-Disposition: form-data; name="data[PrintLabel][upload]"; filename=""
Content-Type: application/octet-stream

Content-Disposition: form-data; name="data[PrintLabel][action]"


Curl POST to CakePHP Form

This is what is working with curl


curl 	--form _method=POST \
	--form 'data[PrintLabel][upload]'=@./ASN_R-34718415X-batch.xml \
	--form 'data[PrintLabel][action]'=print \
	--form 'data[PrintLabel][printer]'=${printer} \

Powershell (version 5.x) POST to CakePHP Form

Important!: The following doesn’t work on Powershell 7

If the file is named powershellPost.ps1 run it as follows:

powershell.exe powershellPost.ps1

$FilePath = "$($pwd)\ASN_R-34718415X-batch.xml";
$URL = '';

$fileBytes = [System.IO.File]::ReadAllBytes($FilePath);
$fileEnc = [System.Text.Encoding]::GetEncoding('ISO-8859-1').GetString($fileBytes);
$boundary = [System.Guid]::NewGuid().ToString(); 
$LF = "`r`n";

$bodyLines = (
    "Content-Disposition: form-data; name=`"_method`"",
    "Content-Disposition: form-data; name=`"data[PrintLabel][printer]`"",
    "Content-Disposition: form-data; name=`"data[PrintLabel][upload]`"; filename=`"hijames.txt`"",
    "Content-Type: application/octet-stream",
    "Content-Disposition: form-data; name=`"data[PrintLabel][action]`"$LF",
) -join $LF

Invoke-RestMethod -Uri $URL -Method Post -ContentType "multipart/form-data; boundary=`"$boundary`"" -Body $bodyLines


Submit a Comment

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

The reCAPTCHA verification period has expired. Please reload the page.

You May Also Like…

Clear HSTS Settings in CHrome

Open chrome://net-internals/#hsts enter the domain in the query field and click Query to confirm it has HSTS settings...

Ubuntu on Hyper-v

It boils town to installing linux-azure # as root or sudo apt-get update apt-get install linux-azure...