{“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))