[Go] Upload an image with the legacy Slack API

Created on November 30, 2026

This historical example shows how to send an image using the retired Slack files.upload method. Slack now documents the external file upload flow. The sample keeps error handling brief.

package main

import (
    "bytes"
    "io"
    "mime/multipart"
    "net/http"
    "os"
)

func main() {
    f, err := os.Open("./hoge.png")
    if err != nil {
        panic("error")
    }
    defer f.Close()

    bodyBuf := &bytes.Buffer{}
    writer := multipart.NewWriter(bodyBuf)
    part, err := writer.CreateFormFile("file", "some-filename")
    if err != nil {
        panic("error")
    }
    if _, err := io.Copy(part, f); err != nil {
        panic("error")
    }

    err = writer.WriteField("token", "dummy-token")
    if err != nil {
        panic("error")
    }

    err = writer.WriteField("channels", "some-channel-name")
    if err != nil {
        panic("error")
    }

    err = writer.Close()
    if err != nil {
        panic("error")
    }

    requestSlack, err := http.NewRequest(
        "POST",
        "https://slack.com/api/files.upload",
        bodyBuf)
    if err != nil {
        panic("error")
    }

    requestSlack.Header.Set("Content-Type", writer.FormDataContentType())

    clientSlack := new(http.Client)
    responseSlack, err := clientSlack.Do(requestSlack)
    if err != nil {
        panic("error")
    }
    defer responseSlack.Body.Close()
}

The example opens ./hoge.png using os.Open. It creates a multipart/form-data writer, adds a file part named some-filename, and copies the image bytes into that part with io.Copy.

It adds the API token as token and a channel name or ID as channels using WriteField. The request uses the Content-Type generated by FormDataContentType so the header includes the correct boundary. An http.Client then sends the request.

Go