2
votes

I'm trying to send a post request to a php file that has post data and a file upload, here is the contents of what I am sending, the code for setting the headers, the php file I have, and what I am getting back from the php file: (the [image file content] is a placeholder for the binary data of the image file I am sending)

My problem is that neither $_POST & $_FILES are showing empty arrays. I am trying to figure out how to fix that.

//data I am sending to the server:

--boundary
content-disposition: post; name='param1'

value1
--boundary
content-disposition: post; name='field2'

value2
--boundary
content-disposition: post; name='field3'

value3
--boundary
content-disposition: form-data; name='file'; filename='app/native/assets/sampleFile.jpg'
Content-Type: image/jpg
Content-Transfer-Encoding: binary

[image file content]
--boundary--

-----------------------------------------------------

for my headers, I have:

request.setRawHeader("Content-Type", "mulipart/form-data, boundary=boundary");
//data holds the string above
request.setRawHeader("Content-Length", QString::number(data.length()).toAscii());

php file:

<?php
  echo "\npost:\n\n";
  print_r($_POST);

  echo "\nfiles:\n\n";
  print_r($_FILES);
?>

result from php file:

post:

Array
(
)

files:

Array
(
)

1
I had an issue with this myself a couple weeks ago. Have you tried sending a file only or post data only to see if they show up? We ended up finding that php.ini needed to be set to accept larger files.Stegrex
your multipart/form-data is mulipart. try var_dumping the $_REQUEST variablepocesar
What environment are you sending the request from?kontur

1 Answers

3
votes

Try something like

--boundary
Content-Disposition: form-data; name="param1"

value1
--boundary
Content-Disposition: form-data; name="field2"

value2
--boundary
Content-Disposition: form-data; name="field3"

value3
--boundary
Content-Disposition: form-data; name="file"; filename="app/native/assets/sampleFile.jpg"
Content-Type: image/jpg
Content-Transfer-Encoding: binary

[image file content]
--boundary--
request.setRawHeader("Content-Type", "multipart/form-data; boundary=boundary");