POSTing a file to a server via an API

I have this CURL command which works to POST a file to a server:

curl -X POST -F 'username=someone' -F 'package=@/Users/me/Desktop/test.zip' http://example.com/api/test

I can’t for the life of me figure out how to convert this simple CURL command (which I’ve verified works) to either the CURLMBS class (I have an MBS licence) or the HTTPSecureSocket class.

Can anyone help me at all? The documentation for both seems enormously complex and I’m banging my head against a brick wall here.

This POSTs a multipart/form-data HTTP form with two elements: a username and the path to a file for upload.

I don’t use the MBS plugin, but I think you can do this using the “FormAdd” family of methods.

The HTTPSecureSocket class doesn’t support multipart/form-data out of the box, but it’s not hard to do.

Thanks Andrew. I got it working with the CURLNMBS class:

[code]’ Set the URL for CURL.
curl.OptionURL = http://example.com/api/test

’ Construct the form to POST
curl.FormAddField(“username”, “bobby”)
curl.FormAdd(curl.kFormCopyName, “package”, curl.kFormFile, “/Users/me/Desktop/test.zip”)
curl.FormFinish()

’ Do the POST.
call curl.Perform()[/code]

Note that I’m using version 18.1 of the plugin (pre-release). That version adds the FormAddField helper method which is a little less verbose.

Thanks also for the link for using native classes. That does look a little unwieldy though.

Hi Garry,

This option is not using the MBS Class… but maybe you find it useful. Please, take a look at the “PDF File Generation? There is an API for that!” blog post on Xojo’s blog, specially the section “Encoding the Post Request … the right way”. This shows how you can do the same as with the CURL command, but using 100% Xojo code.

Hope it helps.

Javier

Another good link Javier. Thank you.