-1

is there anyone can help me to converting this cURL command to Go?

curl -X PUT -H 'Content-Type: image/jpg' \
     -H "Content-Length: 132093" \
     -T "/Users/ikmal/Downloads/catcute.jpg" \
     "https://hootsuite-video.s3.amazonaws.com/production/18395606_a9245dd7-73d6-4392-af4a-1cd9bf359cfb.jpg?AWSAccessKeyId=AKIAIM7ASX2JTE3ZFAAA&Expires=1543304067&Signature=n9sZkQ%2BF1DGuiYHqixrrvmoxIXQ%3D"

I already try generator like:

1. https://mholt.github.io/curl-to-go/

it created Go code like this:

// Generated by curl-to-Go: https://mholt.github.io/curl-to-go

req, err := http.NewRequest("PUT", "https://hootsuite-video.s3.amazonaws.com/production/18395606_a9245dd7-73d6-4392-af4a-1cd9bf359cfb.jpg?AWSAccessKeyId=AKIAIM7ASX2JTE3ZFAAA&Expires=1543304067&Signature=n9sZkQ%2BF1DGuiYHqixrrvmoxIXQ%3D", nil)
if err != nil {
    // handle err
}
req.Header.Set("Content-Type", "image/jpg")
req.Header.Set("Content-Length", "132093")

resp, err := http.DefaultClient.Do(req)
if err != nil {
    // handle err
}
defer resp.Body.Close()

2. https://curl.trillworks.com/

it created Go code like this:

package main

import (
    "fmt"
    "io/ioutil"
    "log"
    "net/http"
)

func main() {
    client := &http.Client{}
    req, err := http.NewRequest("PUT", "https://hootsuite-video.s3.amazonaws.com/production/18395606_a9245dd7-73d6-4392-af4a-1cd9bf359cfb.jpg?AWSAccessKeyId=AKIAIM7ASX2JTE3ZFAAA&Expires=1543304067&Signature=n9sZkQ%2BF1DGuiYHqixrrvmoxIXQ%3D", nil)
    if err != nil {
        log.Fatal(err)
    }
    req.Header.Set("Content-Type", "image/jpg")
    req.Header.Set("Content-Length", "132093")
    resp, err := client.Do(req)
    if err != nil {
        log.Fatal(err)
    }
    bodyText, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        log.Fatal(err)
    }
    fmt.Printf("%s\n", bodyText)
}

But neither gives me proper code because when I run those on project, it returning some error, because it doesn't generate this part:

-T "/Users/ikmal/Downloads/catcute.jpg"

and generate it as "nil" instead.

UPDATE :

What I have done for this problem is adding this code:

file, err := os.Open("/Users/ikmal/Downloads/catcute.jpg")
if err != nil {
    panic(err)
}
defer file.Close()

so I put the variable "file" into body request, here is my final code:

func main(){
    file, err := os.Open("/Users/ikmal/Downloads/catcute.jpg")
    if err != nil {
        panic(err)
    }
    defer file.Close()

    client := &http.Client{}
    req, err := http.NewRequest("PUT", "https://hootsuite-video.s3.amazonaws.com/production/18395606_a9245dd7-73d6-4392-af4a-1cd9bf359cfb.jpg?AWSAccessKeyId=AKIAIM7ASX2JTE3ZFAAA&Expires=1543304067&Signature=n9sZkQ%2BF1DGuiYHqixrrvmoxIXQ%3D", file)
    if err != nil {
        log.Fatal(err)
    }
    req.Header.Set("Content-Type", "image/jpg")
    req.Header.Set("Content-Length", "132093")
    resp, err := client.Do(req)
    if err != nil {
        log.Fatal(err)
    }
    bodyText, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        log.Fatal(err)
    }
    fmt.Printf("%s\n", bodyText)
}

then when I build and run this program, it returning response like this:

<?xml version="1.0" encoding="UTF-8"?>
<Error>
   <Code>NotImplemented</Code>
   <Message>A header you provided implies functionality that is not implemented</Message>
   <Header>Transfer-Encoding</Header>
   <RequestId>FBD2CEAF71EA4DEA</RequestId>
   <HostId>K6hDrHIJr5YtoIBn2d64bfuLBgs6F17gKQV9jrTJ31X987A5gshhqtnKDs3lW2uSliBJwk1pri4=</HostId>
</Error>

It seems the error came from headers problem, but I'm sure I type the header correctly. Is there something wrong that I missed?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Backbone93
  • 11
  • 2

1 Answers1

0

You must using multipart to upload content of image.

f, err := os.Open("./Users/ikmal/Downloads/catcute.jpg")
if err != nil{
    log.Fatal(err)
}
defer f.Close()

var buf = new(bytes.Buffer)
writer := multipart.NewWriter(buf)

part, _ := writer.CreateFormFile("image", "dont care about name")
io.Copy(part, f)

writer.Close()

req, _ := http.NewRequest("POST", "https://hootsuite-video.s3.amazonaws.com/production/18395606_a9245dd7-73d6-4392-af4a-1cd9bf359cfb.jpg?AWSAccessKeyId=AKIAIM7ASX2JTE3ZFAAA&Expires=1543304067&Signature=n9sZkQ%2BF1DGuiYHqixrrvmoxIXQ%3D", buf)
req.Header.Set("Content-Type", writer.FormDataContentType())

client := &http.Client{}
res, _ := client.Do(req)
defer res.Body.Close()
b, _ := ioutil.ReadAll(res.Body)
fmt.Println(string(b))
KibGzr
  • 2,053
  • 14
  • 15