-
Notifications
You must be signed in to change notification settings - Fork 29
Uploading Images API POST Request
Edison Hua edited this page Feb 11, 2023
·
1 revision
This tutorial will cover how to use ImagePut to send image data to APIs.
There are 3 general types of API requests.
- binary file - Smallest data size.
- base64 data - 33% extra data, but easier to work with.
- multipart/form-data - Used for sending multiple images in a single request.
A: As long as the original data source is a clipboard_png|pdf|url|file|stream|RandomAccessStream|hex|base64
.
Imgur_UploadFromClipboard(ClientID := "fbf77ff49c42c8a") {
body := imageputsafearray("cats.jpg")
whr := ComObjCreate("WinHttp.WinHttpRequest.5.1")
; whr.SetProxy(2, "localhost:1080")
whr.Open("POST", "https://api.imgur.com/3/image", true)
whr.SetRequestHeader("Authorization", "Client-ID " . ClientID)
whr.Send(body)
whr.WaitForResponse()
if RegExMatch(whr.ResponseText, """link"":""\K[^""]+", result) {
return StrReplace(result, "\")
} else {
RegExMatch(whr.ResponseText, """error"":""\K[^""]+", errMsg)
throw errMsg ? errMsg : "Unkown Error"
}
}
Run % Imgur_UploadFromClipboard()
Use ImagePutURI() which generates the MIME type. Use ImagePutBase64() is base64 data is only desired.
Most API endpoints will smartly figure out the contents of the file by searching for magic bytes. This ensures that if there is a conflict between the MIME type
data:image/png;base64
and the contents of the actual file, it is resolved in favor of the file.
ImagePutFormData has not yet been implemented.
My personal version is here: https://www.autohotkey.com/boards/viewtopic.php?p=480935#p480935