Created
October 30, 2023 19:34
-
-
Save BackgroundCut/8bfbc5274866567dab5612d3e7244152 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bytes" | |
"encoding/json" | |
"fmt" | |
"io" | |
"io/ioutil" | |
"net/http" | |
"time" | |
) | |
const ( | |
apiEndpoint = "https://backgroundcut.co/api/v1/cut/" | |
apiKey = "YOUR-API-KEY" | |
timeoutDuration = 20 * time.Second // 20 seconds | |
localFilename = "/path/to/output.webp" | |
) | |
var requestParameters = map[string]string{ | |
"file_url": "https://www.example.com/example.jpg", | |
"max_resolution": "12000000", // 12 MegaPixels (for example 4000 * 3000) | |
"quality": "medium", | |
"return_format": "webp", | |
} | |
func main() { | |
// Authorization header | |
client := &http.Client{Timeout: timeoutDuration} | |
req, err := http.NewRequest("POST", apiEndpoint, nil) | |
if err != nil { | |
fmt.Println("Error creating request:", err) | |
return | |
} | |
req.Header.Add("Authorization", "Token "+apiKey) | |
// Add request parameters to the body | |
q := req.URL.Query() | |
for k, v := range requestParameters { | |
q.Add(k, v) | |
} | |
req.URL.RawQuery = q.Encode() | |
// Make the request | |
resp, err := client.Do(req) | |
if err != nil { | |
fmt.Println("Error making request:", err) | |
return | |
} | |
defer resp.Body.Close() | |
// Handle response | |
if resp.StatusCode == http.StatusOK { | |
var response struct { | |
OutputImageURL string `json:"output_image_url"` | |
} | |
if err := json.NewDecoder(resp.Body).Decode(&response); err != nil { | |
fmt.Println("Error decoding JSON response:", err) | |
return | |
} | |
outputImageURL := response.OutputImageURL | |
// Download the output image | |
outputImageResp, err := http.Get(outputImageURL) | |
if err != nil { | |
fmt.Println("Error downloading image:", err) | |
return | |
} | |
defer outputImageResp.Body.Close() | |
if outputImageResp.StatusCode != http.StatusOK { | |
fmt.Printf("Error downloading image. Status Code: %d\n", outputImageResp.StatusCode) | |
return | |
} | |
// Save the image | |
outputImageFile, err := os.Create(localFilename) | |
if err != nil { | |
fmt.Println("Error creating output image file:", err) | |
return | |
} | |
defer outputImageFile.Close() | |
if _, err = io.Copy(outputImageFile, outputImageResp.Body); err != nil { | |
fmt.Println("Error saving image:", err) | |
return | |
} | |
fmt.Println("Image saved successfully!") | |
} else if resp.StatusCode >= 400 && resp.StatusCode < 500 { | |
bodyBytes, _ := ioutil.ReadAll(resp.Body) | |
bodyString := string(bodyBytes) | |
fmt.Printf("Client error. Status Code: %d. Error Message: %s\n", resp.StatusCode, bodyString) | |
} else if resp.StatusCode >= 500 && resp.StatusCode < 600 { | |
fmt.Printf("Server error. Status Code: %d\n", resp.StatusCode) | |
} else { | |
fmt.Printf("Unexpected response. Status Code: %d\n", resp.StatusCode) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment