I'm creating a HTTP PUT request manualy. I have the following format
POST http://server.com/id/55/push HTTP/1.0
Content-type: multipart/form-data, boundary=AaB03x
Content-Length: 168
--AaB03x
Content-Disposition: form-data; name="image"; filename="small.jpg"
Content-Type: image/jpeg
Content-Transfer-Encoding: binary
<file content>
--AaB03x--
My question is, how should I fill the "file content" area? If I open a jpeg with TexMate or with cat command line application and I paste the ASCII output, the request does not work.
UPDATE
I'm working with a microprocessor and I can't use C or a high level language I need to manually do the raw request. Do I need to separate with spaces every binary byte read from the file?
In case of saving the jpg into a file in the server side, Do I have to convert the binary stream to ASCII?
I read the binary code of a JPG from my hard drive with a simple php conde:
$filename = "pic.jpg";
$handle = fopen($filename, "rb");
$fsize = filesize($filename);
$contents = fread($handle, filesize($filename));
fclose($handle);
//echo $contents;
for($i = 0; $i < $fsize; $i++)
{
// get the current ASCII character representation of the current byte
$asciiCharacter = $contents[$i];
// get the base 10 value of the current characer
$base10value = ord($asciiCharacter);
// now convert that byte from base 10 to base 2 (i.e 01001010...)
$base2representation = base_convert($base10value, 10, 2);
// print the 0s and 1s
echo($base2representation);
}
whit this code I get a stream of 1 and 0. I can send it including the string of 101010101... to where the tag "file content" of my manually http request is but in the server side I can't visualise the JPG... ¿should I convert it to ASCII again?
SOLUTION
Okay the solution was very simple, I just dumped the ASCII code into the tag "file content" of the http request. Despite I'm using a micro controller I opened a socket with PHP and tested out. The solution was to read the ASCII from the file instead of paste directly the ASCII into the code.
Here a working example of the solution:
<?php
//We read the file from the hard drive
$filename = "pic.jpg";
$handle = fopen($filename, "rb");
$fsize = filesize($filename);
$contents = fread($handle, filesize($filename));
fclose($handle);
$mesage = $contents;
//A trick to calculate the length of the HTTP body
$len = strlen('--AaB03x
Content-Disposition: form-data; name="image"; filename="small.jpg"
Content-Type: image/jpeg
Content-Transfer-Encoding: binary
'.$mesage.'
--AaB03x--');
//We create the HTTP request
$out = "POST /temp/test.php HTTP/1.0\r\n";
$out .= "Content-type: multipart/form-data boundary=AaB03x\r\n";
$out .= "Content-Length: $len\r\n\r\n";
$out .= "--AaB03x\r\n";
$out .= "Content-Disposition: form-data; name=\"image\"; filename=\"small.jpg\"\r\n";
$out .= "Content-Type: image/jpeg\r\n";
$out .= "Content-Transfer-Encoding: binary\r\n\r\n";
$out .= "$mesage\r\n";
$out .= "--AaB03x--\r\n\r\n";
//Open the socket
$fp = fsockopen("127.0.0.1", 8888, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
//we send the message thought the opened socket
fwrite($fp, $out);
while (!feof($fp)) {
echo fgets($fp, 128);
}
fclose($fp);
}
//Visualize the query sent
echo nl2br($out);
?>
In the real implementation I will simple read directly from the memory of the microcontroler just as I did in the php