Skip to content

POST and Upload

Qriist edited this page Sep 17, 2026 · 2 revisions

LibQurl provides two methods for sending data, each targeting a different use case:

Method Use case Accepted sources
SetPost() POST request body String, Integer, Object, Array, Map, Buffer, File
SetUpload() PUT-style upload, byte-range capable File, Buffer*

Both are followed by Sync() or Async() to perform the transfer.

*Buffer support in SetUpload is planned but requires some extensions to the Storage class.

POST

SetPost prepares the request body and sets the appropriate libcurl options. It accepts almost any source type and handles the encoding internally.

Note that you aren't limited to POST transfers. You're able to SetOpt a different http verb via CUSTOMREQUEST at any point after the SetPost.

Source Types

curl.SetOpt("URL", "https://httpbin.org/post")

; Integer
curl.SetPost(1234567890)
curl.Sync()

; String
curl.SetPost("abcdefghij")
curl.Sync()

; Object (dumped as JSON)
data := { key: "value", count: 42 }
curl.SetPost(data)
curl.Sync()

; Array (dumped as JSON)
curl.SetPost(["item1", "item2"])
curl.Sync()

; Map (dumped as JSON)
curl.SetPost(Map("key", "value"))
curl.Sync()

; Buffer
buf := Buffer(17, 81)
curl.SetPost(buf)
curl.Sync()

; File
file := FileOpen("C:\path\to\upload.bin", "r")
curl.SetPost(file)
curl.Sync()

String and Integer are sent as-is. Object, Array, and Map are dumped to JSON via cJson. File and Buffer are sent as binary.

Return Value

SetPost returns the detected MIME type string. This is useful when the endpoint requires an explicit Content-Type:

mimeType := curl.SetPost(data)
curl.SetOpt("CONTENT_TYPE", mimeType)
curl.Sync()

Object, Array, and Map sources always return "application/json". Other types are detected via libmagic.

Upload

SetUpload is for PUT-style uploads where you need byte-range control. It accepts File sources only.

As with SetPost, you aren't limited to PUT verbs for this transfer. You may call curl.SetOpt("CUSTOMREQUEST",<verb>) after invocation to change the request type, though it's on the user to understand if any verb changes are compatible with the remote server.

Signature

SetUpload(sourceData, startByte?, numBytes?, easy_handle?)
  • startByte — offset into the source. Default 0.
  • numBytes — number of bytes to send. Default is the full source size.
  • Both are clamped to valid bounds automatically.

File Source

easy_handle := curl.EasyInit()
curl.SetOpt("URL", "https://httpbin.org/put")
curl.SetOpt("CUSTOMREQUEST", "PUT")

file := FileOpen("C:\path\to\upload.bin", "r")
curl.SetUpload(file, 1024, 4096)
; send bytes 1024 through 5119 of the file

curl.WriteToFile("C:\path\to\response.json")
curl.Sync()
curl.EasyCleanup()

autoResetToGET

When uploading, disable the automatic POST/UPLOAD state reset so the handle retains its upload configuration:

curl.autoResetToGET := 0

autoResetToGET is currently a class-wide convention but will be made per-handle in the future.

See Terminology and Conventions for details.

Clearing POST / Upload State

ClearPost scrubs all POST and UPLOAD options from the handle and resets CUSTOMREQUEST to "GET". Use it when switching between upload and download on the same handle:

curl.SetPost("some data")
curl.Sync()

; Switch back to a plain GET on the same handle
curl.ClearPost()
curl.SetOpt("URL", "https://example.com")
curl.Sync()

If autoResetToGET is enabled (default), this happens automatically after each transfer.

Where to Go Next

Clone this wiki locally