Unable to create image using catalog-api

{“errors”:[{“category”:“INVALID_REQUEST_ERROR”,“code”:“INVALID_CONTENT_TYPE”,“detail”:“Only [image/jpeg image/pjpeg image/png image/x-png image/gif] content type(s) allowed but got application/octet-stream”}]}

// Open the image file
file, err := os.Open(“1.jpeg”)
if err != nil {
fmt.Println(“Error opening image file:”, err)
return
}
defer file.Close()

// Create a new multipart writer
var requestBody bytes.Buffer
writer := multipart.NewWriter(&requestBody)

// Add the image file to the request body
part, err := writer.CreateFormFile("file", "1.jpeg")
if err != nil {
	fmt.Println("Error creating form file:", err)
	return
}
_, err = io.Copy(part, file)
if err != nil {
	fmt.Println("Error copying file to form file:", err)
	return
}

// Add the JSON request data
requestJSON := []byte(`{
    "idempotency_key": "528dea59--43cbd48-4a6bba7dd61f86",
    "image": {
        "id": "#TEMP_IsD",
        "type": "IMAGE",
        "image_data": {
            "name": "Capsicusm"
        }
    }
}`)
writer.WriteField("request", string(requestJSON))

// Close the multipart writer
err = writer.Close()
if err != nil {
	fmt.Println("Error closing multipart writer:", err)
	return
}

// Create the HTTP request
req, err := http.NewRequest("POST", "https://connect.squareup.com/v2/catalog/images", &requestBody)
if err != nil {
	fmt.Println("Error creating request:", err)
	return
}
req.Header.Set("Square-Version", "2024-02-22")
req.Header.Set("Authorization", "Bearer <Token>")
req.Header.Set("Accept", "application/json")
req.Header.Set("Content-Type", writer.FormDataContentType())
log.Println(writer.FormDataContentType())
// Send the request
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
	fmt.Println("Error sending request:", err)
	return
}
defer resp.Body.Close()

// Read and display the response body
responseBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
	fmt.Println("Error reading response body:", err)
	return
}
fmt.Println(string(responseBody))

The error message you’re receiving indicates that the server expects the content type of the uploaded file to be one of the following image formats: JPEG, PNG, or GIF. However, the content type being sent is application/octet-stream, which is a generic binary stream and not specific to image files.

Looking at your code, it appears you’re properly setting the Content-Type header for the multipart form data, but you might not be setting the Content-Type for the file part itself. The server needs to know the MIME type of the file being uploaded, which should be set in the CreateFormFile call or as a header in the file part.

Here’s how you can modify your code to set the content type for the image file:

// ...

// Add the image file to the request body
part, err := writer.CreateFormFile("file", "1.jpeg")
if err != nil {
	fmt.Println("Error creating form file:", err)
	return
}

// Set the Content-Type header for the image file
partHeader := make(textproto.MIMEHeader)
partHeader.Set("Content-Disposition", `form-data; name="file"; filename="1.jpeg"`)
partHeader.Set("Content-Type", "image/jpeg") // Set the correct MIME type for the file

part, err = writer.CreatePart(partHeader)
if err != nil {
	fmt.Println("Error creating form part with header:", err)
	return
}

// ...

// The rest of your code remains the same

Make sure to replace "image/jpeg" with the correct MIME type if the file is a PNG or GIF.

Additionally, ensure that the file field name you use in CreateFormFile matches the expected field name on the server side.

After making these changes, your code should correctly set the MIME type for the image file, and the server should accept the upload if everything else is correct. :slightly_smiling_face:

1 Like

Thank you very much Bryan, I was stuck on this for quite sometime. :slight_smile: :slight_smile: