HTTPSocket.post to send a file to a webserver

I’ve been searching the docs, but can’t find anything that touches on it.
I’d like to do the equivalent of an html post to send a file to a webserver. (PHP page would handle the post on the webserver).
I get that Xojo wants a dictionary for the setformdata function.
I don’t think I can put a folderitem in a dictionary, can I? Maybe I could somehow convert the file to text with some type of encoding and put it in the dictionary that way? I don’t see a uuencode function in the language reference.
Any ideas?
Thanks
Bernn

Read the contents of the file into a string with TextinputStream.ReadAll (it will work for binary data as well), then use HTTPSocket.SetPostContent.

I’m getting close, but can’t find a lot of help out there on the internet in the way of examples.

I’ve read it into a string and posted it:

dim H as new HTTPSocket
  dim f as  FolderItem
  dim i as Boolean
  Dim t As TextInputStream
  f = GetFolderItem("Z:\\surfacefinish.pdf")
  t = TextInputStream.Open(f)
  dim s as string = t.ReadAll
  H.SetRequestContent(s,"application/pdf")
  H.post("http://jweb/bin/test.php")

On my PHP page, I can’t seem to access it. It’s not in the $_REQUEST array or the $_FILES array.
Here’s what the $_SERVER array looks like. The file size is right, so it seems to have sent it. It has the content_type as well, so it seems like it did it.
How do I get the content I posted?
Thanks,
Bernn

[quote] [HTTP_ACCEPT] => /
[HTTP_ACCEPT_LANGUAGE] => en
[CONTENT_LENGTH] => 77411
[HTTP_HOST] => jweb
[CONTENT_TYPE] => application/pdf
[PATH] => /usr/local/bin:/usr/bin:/bin
[SERVER_SIGNATURE] => Apache/2.2.22 (Ubuntu) Server at jweb Port 80

[SERVER_SOFTWARE] => Apache/2.2.22 (Ubuntu)
[SERVER_NAME] => jweb
[SERVER_ADDR] => 192.168.1.100
[SERVER_PORT] => 80
[REMOTE_ADDR] => 192.168.1.21
[DOCUMENT_ROOT] => /home/joanne
[SERVER_ADMIN] => webmaster@localhost
[SCRIPT_FILENAME] => /home/joanne/bin/test.php
[REMOTE_PORT] => 57814
[GATEWAY_INTERFACE] => CGI/1.1
[SERVER_PROTOCOL] => HTTP/1.0
[REQUEST_METHOD] => POST
[QUERY_STRING] => 
[REQUEST_URI] => /bin/test.php
[SCRIPT_NAME] => /bin/test.php
[PHP_SELF] => /bin/test.php
[REQUEST_TIME] => 1409370215[/quote]

Does the PHP script expect an HTML form containing the file, or just the file itself?

$data = $HTTP_RAW_POST_DATA;

Thank TIm!

That works for me.
I also tried to put the string in a dictionary, but I have a problem with the PHP side. It seems to work but keeps truncating the file at a consistent point. I only get part of the data in the file. Maybe a character in the string that bothers PHP? The only problem with not using a dictionary is I don’t have any way to tell the webserver what the file is and what it should do with it.
Any ideas?

Works:
Xojo:

  dim H as new HTTPSocket
  dim f as  FolderItem
  Dim t As TextInputStream
  
  f = GetFolderItem("Z:\\surfacefinish.pdf")
  t = TextInputStream.Open(f)
  dim s as string = t.ReadAll
  H.SetRequestContent(s,"application/pdf")  
  H.post("http://jweb/bin/test.php")

PHP:

<?php
$filename=$_REQUEST['filename'];
file_put_contents("../testMe.pdf", $HTTP_RAW_POST_DATA);
?>

Doesn’t work:
XOJO:

dim H as new HTTPSocket
  dim f as  FolderItem
  Dim t As TextInputStream
  
  f = GetFolderItem("Z:\\surfacefinish.pdf")  
  t = TextInputStream.Open(f)  
  dim s as string = t.ReadAll
   dim d as new dictionary
  
  d.Value("ID")="myString"
  d.Value("file")=s
  
  H.SetFormData(d)
  H.post("http://jweb/bin/test2.php")

PHP:

[code]

<?php $fp=fopen("../testnew2.php","w"); fwrite($fp, $_POST['file']); ?>[/code]

Try using SetRequestHeader to set some arbitrary headers that http will ignore but your php script can use to decide what the file is and what to do with it. I don’t know why the dictionary would fail like that. I primarily use this mechanism to send xml to a php script.

Brilliant…
Thanks,
Bernn