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?
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]
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.
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
Thanks for your help. I’m sure it will also help others.
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.