Uploading Images API POST Request - iseahound/ImagePut GitHub Wiki

This tutorial will cover how to use ImagePut to send image data to APIs.

There are 3 general types of API requests.

  1. binary file - Smallest data size.
  2. base64 data - 33% extra data, but easier to work with.
  3. multipart/form-data - Used for sending multiple images in a single request.

Q: Does ImagePut work with text data, application files, or video data?

A: As long as the original data source is a clipboard_png|pdf|url|file|stream|RandomAccessStream|hex|base64.

Sending a binary file to APIs

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() 

Sending base64 data to APIs

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.

Sending multipart/form-data to APIs

ImagePutFormData has not yet been implemented.

My personal version is here: https://www.autohotkey.com/boards/viewtopic.php?p=480935#p480935