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…

Meraki Open Source Licenses

Until today I assumed that Meraki was built in-house with only closed source software. But having a look at the...


If you have Veeam backup failing with the Updating BCD failed with Cannot update SafeBoot flag and SentinelOne is...