Subject: [Dev] [APIM] [Golang] Implementing HTTP PUT in
Golang



Hi,I'm working on creating a CLI tool for importing and exporting APIs between different environments (for WSO2 API Manager 3.0.0) using Golang.
I have an issue with importing APIs.The following curl command works fine.`curl -k -F "[email protected]" -X PUT -H "Authorization: Bearer <access_token>" https://localhost:9292/api/am/publisher/v1.0/import/apis`
The problem is with mapping this to Go code.Here's what I wrote at first.
func main() {    data, err := os.Open("./exported.zip")    accessToken := "<access_token>"    if err != nil {        fmt.Println("Error oening file")        panic(err)    }
    req, reqErr := http.NewRequest(http.MethodPut, "https://localhost:9292/api/am/publisher/v1.0/import/apis", data)    if reqErr != nil {        fmt.Println("Error creating request")        panic(reqErr)    }
    req.Header.Add("Content-Type", "multipart/form-data") // response is 500 with or without this line    req.Header.Add("Authorization", "Bearer "+accessToken)
    tr := &http.Transport{        TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, // for skipping errors in https    }
    client := &http.Client{Transport: tr}    res, resErr := client.Do(req)
    if resErr != nil {        fmt.Println("Error in response")        panic(resErr)    }
    body, err := ioutil.ReadAll(res.Body)
    fmt.Println("Response:", res)    fmt.Println("Body:", string(body))    fmt.Println("Status:", res.Status)
    defer res.Body.Close()}
This returns a 500 Internal Server Error.
But the request works fine with Postman client. Therefore, I tried putting the extra headers generated by Postman in Go code.
func main() {    url := "https://localhost:9292/api/am/publisher/v1.0"    accessToken := "<access_token>"
    payload := strings.NewReader("------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"file\"; filename=\"./exported.zip\"\r\nContent-Type: application/zip\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--")
    req, _ := http.NewRequest(http.MethodPut, url, payload)
    req.Header.Add("content-type", "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW")    req.Header.Add("authorization", "Bearer "+accessToken)    req.Header.Add("cache-control", "no-cache")
    tr := &http.Transport{        TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, // for skipping errors in https    }
    client := &http.Client{Transport: tr}    resp, _ := client.Do(req)
    if resp != nil {
        body, _ := ioutil.ReadAll(resp.Body)
        fmt.Println(resp)        fmt.Println(string(body))    } else {        fmt.Println("Null Response")    }
    fmt.Println(resp.Status)}
This returns a 405 - Method Not Allowed
So, Postman works. Curl works. Go does not.I googled on 'multipart/form-data' under HTTP, but that didn't help either.

Your advice is much appreciated in solving this issue.
Thanks and Regards
Menuka WarushavithanaSoftware Engineering InternWSO2
Moblie:      + 94 77 6979690

_______________________________________________
Dev mailing list
[email protected]
http://wso2.org/cgi-bin/mailman/listinfo/dev



Programming list archiving by: Enterprise Git Hosting