URLConnection POST empties the file after sending?!

I have an app that is sending a file to a server, using URLConnection with POST. The file is correctly sent to destination, but the original of the file on my hard drive is emptied. This is using Xojo 2019 R3.1. The code is simple, here’s a fragment:

Dim f as FolderItem
Dim FilenameStr as string

f=SpecialFolder.Desktop.Child(“TEST”).Child(“Test File from Lightwright.xml”) 'This folder exists on the hard drive and has content
If f<>Nil And f.Exists And f.IsReadable Then
gURLConnection.Send(“POST”, “https://mybeta.test-app.com/lw?email="+EmailAddrStr+"&file=”+FilenameStr, f, 60)
End If[/code]

The URLConnection.FileReceived event is not raised, so I would expect my original copy of the file to be intact.

Is emptying the file intended behavior? The documentation doesn’t mention it.

You’re not sending a file here, you’re saving the response of the POST to the file, if you want to send a file to that address, see URLConnection.SetRequestContent

Oops! My bad - definitely time for CURL. Thank you for the quick reply!

I looked at that documentation, but still don’t know how to use that to send a file or its content to a server. Can you recommend a tutorial somewhere that would make it clear to a HTTP newbie like me?

To upload a file with additional parameters, like it appears you want to do here, you need to use multipart form data. You’d need to use SetRequestContent with the content type of multipart/form-data; charset=utf-8; boundary=SOMERANDOMBOUNDARY and a body similar to

Content-Disposition: form-data; name=“email”

Content-Disposition: form-data; name=“file”; filename=“testfile.txt”
Content-Type: text/plain

test content

Making sure all the SOMERANDOMBOUNDARY values match. The body is simple to generate for static usages, but gets pretty hairy when trying to write a general purpose multipart builder. Also note you would leave off the ?email=blahblah query parameters from the URI. Those are not technically supposed to be included in POST requests, but most servers will respect them anyway. Still, it’s better to include the email in the multipart data. Oh and you have to use CRLF for newlines, which aren’t presented well here of course. Whitespace does matter for this.

Yeah I know, it’s not pleasant is it? Too bad URLConnection doesn’t have a built-in mechanism for this.