I have a Curl command to upload an image, works fine and it reads like:
curl -F “imagekey=@C:/Temp/img01.jpg” -H ‘Accept: application/json’ -H ‘Content-Type: application/json’ -u username:userpassword https://webapi.com/images.json
I try to use Curlsmbs and my code is:
[code]
const CURLFORM_COPYNAME = 1
const CURLFORM_FILE = 10
const CURLFORM_CONTENTTYPE = 14
dim header(-1) as string
header.Append “Accept: application/json”
header.Append “Content-Type: application/json”
Dim MyCurl as CURLSMBS = new myCurlUpload //just a CURLSMBS with debug and progress
MyCurl.SetOptionHTTPHeader header
MyCurl.OptionUsername = username
MyCurl.OptionPassword = userpassword
MyCurl.OptionURL = “https://webapi.com/images.json”
MyCurl.OptionVerbose = True
MyCurl.CollectDebugData = True
MyCurl.CollectOutputData = true
MyCurl.YieldTime = True
MyCurl.FormAdd CURLFORM_COPYNAME, “imagekey”, CURLFORM_FILE, f.UnixpathMBS
MyCurl.FormFinish
Dim nReturn as integer = MyCurl.Perform[/code]
If the file is a large one the process simply hangs at 2%, otherwise it finishes with “400 - Bad request” and “HTTP error before end of send, stop sending”. Have tried setting contenttype but it doesn’t helps, anyway debug log says correct type.
Have tried also reading file in a buffer and sending this with kFormBufferPtr but same result.
Any help would be appreciated.
I would recommend to load file in memory and add as buffer.
That works better.
Christian, I already did:
dim b as BinaryStream
b = BinaryStream.Open(f)
dim buf as string
buf=b.Read(b.Length)
MyCurl.FormAdd(MyCurl.kFormCopyName,"imagekey",MyCurl.kFormBuffer,f.Name,MyCurl.kFormBufferPtr,buf,MyCurl.kFormBufferLength,lenb(buf))
But result is the same.
debug messages?
You could use URL http://monkeybreadsoftware.de/filemaker/echo.cgi to send to my echo cgi script.
It will return back the form data for review.
By removing the header at “MyCurl.SetOptionHTTPHeader” I have got it working fine in Windows both with buffer and without (testing with small files).
BUT in Mac it doesn’t works, the exact same code and file.
- If using buffer it takes for ever and finally freezes at 69%, I don’t get debug details in Xojo.
- If not using buffer the answer is 413 Request Entity Too Large
Sending to your echo Url doesn’t works, it starts a loop and progress ultotal is very high (17*10^12)
the cgi probably only works well with up to 8 MB as the server may put a limit on this.
The image file is only 3KB.
Any reason why it works in Windows and not in Mac?
To update the info, it doesn’t works with larger files (around 10Mb), not in Mac and neither Windows. It remains at 2% and doesn’t progress, in Windows I have to use Task manager to close the app.
Any alternative?
maybe you start debugging it with me by sending me a test project?
With valid credentials please.
Okay. Please don’t to DoEvents in events.
Also printing all data in DebugMessage event is not useful. Better just pick debug messages on the end from DebugData property.
It looks like the CURL has a bug here:
Content-Length: 17592186044610
it doesn’t produce right length for content stream.
d.OptionPost = true
Dim b as BinaryStream = BinaryStream.Open(f)
Dim buf as string = b.Read(b.Length)
d.FormAdd(d.kFormCopyName,"spread[image]", d.kFormBuffer, f.Name, d.kFormBufferPtr, buf, d.kFormBufferLength, lenb(buf))
this works better.
Thanks Christian, it is working fine now in buffer mode after changing the debug event.