converting code from PHP into xojo Httpsocket requests

I’ve been working on a web app using httpsocket. I need to upload the contents of a file to a server. I have an example of the uploading code in PHP, but I’m not sure how to convert it to Xojo. The PHP code is:

$target_url = https://www18.online-convert.com/upload-file/f47f533b-83fa-11e5-9d07-002590d8633c
$file_name_with_full_path = realpath(’./sample.jpg’);
$post = array(‘extra_info’ => ‘123456’,‘file_contents’=>’@’.$file_name_with_full_path);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$result=curl_exec ($ch);
curl_close ($ch);

I understand most of the above code, but I don’t know how to handle the array that is being created in the 3rd line and then sent to the server in the 7th and 8th lines. Can anybody advise how to turn this into the appropriate SetRequestHeader and/or SetRequestContent statements?

It looks just like an http post to the target_url with 123456 in the extra_info field and the file data in file_contents.

Seems like you should be able to do a HTTPSocket.Post along the lines of the examples in the language reference.

[quote=227780:@Kevin Clark]I’ve been working on a web app using httpsocket. I need to upload the contents of a file to a server. I have an example of the uploading code in PHP, but I’m not sure how to convert it to Xojo. The PHP code is:

$target_url = https://www18.online-convert.com/upload-file/f47f533b-83fa-11e5-9d07-002590d8633c
$file_name_with_full_path = realpath(’./sample.jpg’);
$post = array(‘extra_info’ => ‘123456’,‘file_contents’=>’@’.$file_name_with_full_path);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$result=curl_exec ($ch);
curl_close ($ch);

I understand most of the above code, but I don’t know how to handle the array that is being created in the 3rd line and then sent to the server in the 7th and 8th lines. Can anybody advise how to turn this into the appropriate SetRequestHeader and/or SetRequestContent statements?[/quote]
Ch isn’t an array. It’s an object, like the object you’d get from

dim h as new HTTPSovket

Then lines 4-6 set the url, method and request content. Line 7 is the equivalent of SendRequest.

Not tested.

[code] dim target_url as String = “https://www18.online-convert.com/upload-file/f47f533b-83fa-11e5-9d07-002590d8633c
dim file_name_with_full_path as String = GetFolderItem("").Child(“sample.jpg”).NativePath

dim post as new Dictionary
post.Value(“extra_info”) = 123456
post.Value(“file_contents”) = “@” + file_name_with_full_path

HTTPSecureSocket1.SetFormData(post)

// Asynchronous post
//HTTPSecureSocket1.Post(target_url)

// Synchronous post
dim result as String = HTTPSecureSocket1.Post(target_url, 30)[/code]

Edit:
I didn’t see that the thread about web version. The above code only works on Desktop version. I don’t know how to access socket in web version.

Edit 2:

HTTPSecureSocket can be used in web edition. So, may be the code can be implemented.

[quote=228027:@Asis Patisahusiwa][code] dim target_url as String = “https://www18.online-convert.com/upload-file/f47f533b-83fa-11e5-9d07-002590d8633c
dim file_name_with_full_path as String = GetFolderItem("").Child(“sample.jpg”).NativePath

dim post as new Dictionary
post.Value(“extra_info”) = 123456
post.Value(“file_contents”) = “@” + file_name_with_full_path

HTTPSecureSocket1.SetFormData(post)

// Asynchronous post
//HTTPSecureSocket1.Post(target_url)

// Synchronous post
dim result as String = HTTPSecureSocket1.Post(target_url, 30)[/code]
[/quote]
There’s only one problem with your solution… the original code sends the contents of the file. This code simply sends the filename with an @ symbol in front of it.

You’d need to use a binarystream to read the file’s data and put THAT into the file_contents entry.

why not use file_get_contents to read the file?

Never seen and use @ operator before.

http://stackoverflow.com/questions/3551527/php-at-symbol-before-variable-name-post
http://stackoverflow.com/questions/1032161/what-is-the-use-of-symbol-in-php

you could try to convert the CURL calls in PHP directly to CURL calls for MBS CURL Plugin.

There we do have methods to add Form fields for the post.

It turns out that somebody wrote a function to upload files to a server using the HttpSocket control. It’s posted here: http://www.boredomsoft.org/file-uploads-form-encodings-and-xojo.bs

I was able to get my program working using the above code. Thanks to everyone for your answers.