How to upload a file using HTTPSocket post?

Hi All,

I’m trying to upload a file from a local hard disk to a webserver using the HTTPSocket from the latest version of XOJO.
I would like to capture also the JSON reply of the server.

I found some helpful example code in the HTTP POST and HTTP GET, but I don’t see how I can add a file to the header.

When I use curl from command line, this is what I do
curl -X POST -F “file=@test.docx” “https://server.com/api/filestore?varA=X&varB=Y
but I cannot assume curl will work on all the machines (Windows, Mac & Linux).

Does anyone have an example of how to add a file to an HTTPSocket post?

thanks

Gert

as you have a curl command line, you can simply use the MBS CURL Plugin:

[code] dim d as new CURLSMBS
d.OptionURL = “https://server.com/api/filestore?varA=X&varB=Y
d.OptionVerbose = True
d.CollectDebugData = true
d.CollectHeaderData = true
d.CollectOutputData = true
d.OptionPost = true

// read data to send
dim buf as string = stream.Read(stream.Length)

// add a section named filMyFile
// with a file named file.name
// and binary content in buf
d.FormAdd(d.kFormCopyName,“file”, d.kFormBuffer, “test.docx”, d.kFormBufferPtr, buf, d.kFormBufferLength, lenb(buf))
d.FormFinish

dim e as integer = d.Perform

dim ResultText as string = d.OutputData
dim DebugMessages as string = d.DebugData [/code]

Thanks Christian. My assumption is that most users of the tool won’t have CURL.
If this not feasible with the HTTPSocket?

thanks

Gert

Our curl plugin includes the CURL library and also SSH and SSL libraries.

So no need for users to have curl installed.

OK, Christian, I downloaded the CURL plugin for testing.
I get a “This item does not exist error” for stream in: dim buf as string = stream.Read(stream.Length)
I checked the plugin is loaded and it is.

Did I do something wrong?

That is just example code. We need the data to send in buf variable here.
e.g. reading with a Binarystream named stream.

OK, so this is working now

f = GetFolderItem("C:\\Users\\Gert\\Desktop\\Gert_Test.txt") t = TextInputStream.Open(f) dim buf as string = t.ReadAll

thanks for your help.

For future reference, you don’t have to use CURL.
You can use a HTTPSocket and set the POST data.

Maybe, but you need to build the form data yourself including all the file parts.

Easy enough :slight_smile:

Andrew, Tim, this is helpful. In the meantime I already bought the CURL plugin and updated to the new release (an expensive day!) but I may still decide to use this method. That was was I was looking for, but I was impatient :wink:

Thanks for your help. I’m sure it will also help others.

gert

not everything requires a plugin :slight_smile:

CURL is useful for a lot of things like FTP, OAuth, Email and so much more. If we run into issues with the built-in stuff it’s our goto class.

It really is. You still don’t need a plugin, though.

Um…okay. I don’t disagree. The OP has already purchased the MBS plugin. It has a ton of example projects and good support.

My own opinion: you are not doing yourself any favors by creating links like you did above. They tell me nothing. I prefer this style: https://github.com/charonn0/RB-libcURL to it tells me where I’m going. If you had done that I probably would have actually clicked on it. Anyway, now I know and when in need of CURL in Xojo I can consider it.

Sorry.

I for one, am glad you had so much information to share :slight_smile:
Thank you!

Hi Christian, is there a way to make a progressbar show the upload progress using the CURL plugin? – thanks

The CURLSMBS class has a progress event you can use.
Or use a timer and query the progress properties to update bar.

Thanks, Christian.