Hi Isuru,
Unfortunately, it did not work with POST either.

Thanks
Menuka

*Menuka Warushavithana*
*Software Engineering Intern*
*WSO2*

*Moblie:      + <%2B%2094%2011%202145345%20%C2%A0Ext.%205737> 94 77 6979690*



On Mon, Aug 14, 2017 at 9:09 AM, Isuru Haththotuwa <isu...@wso2.com> wrote:

> Just to clarify, is this working with POST?
>
>
> On Mon, Aug 14, 2017 at 9:01 AM, Isuru Haththotuwa <isu...@wso2.com>
> wrote:
>
>> The best way to figure out this would be to debug the MSF4J layer.
>>
>> @Thusitha/Chanaka: Can you please give some input on where to start
>> debugging for this particular case?
>>
>> On Sun, Aug 13, 2017 at 12:54 PM, Menuka Warushavithana <men...@wso2.com>
>> wrote:
>>
>>> 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 "file=@exported.zip" -X PUT -H "Authorization: Bearer
>>> <access_token>" https://localhost:9292/api/am/publisher/v1.0/import/apis
>>> <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("------WebKi
>>> tFormBoundary7MA4YWxkTrZu0gW\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 Warushavithana*
>>> *Software Engineering Intern*
>>> *WSO2*
>>>
>>> *Moblie:      + <%2B%2094%2011%202145345%20%C2%A0Ext.%205737> 94 77
>>> 6979690 <077%20697%209690>*
>>>
>>>
>>>
>>
>>
>> --
>> Thanks and Regards,
>>
>> Isuru H.
>> +94 716 358 048 <071%20635%208048>* <http://wso2.com/>*
>>
>>
>>
>
>
> --
> Thanks and Regards,
>
> Isuru H.
> +94 716 358 048 <+94%2071%20635%208048>* <http://wso2.com/>*
>
>
>
_______________________________________________
Dev mailing list
Dev@wso2.org
http://wso2.org/cgi-bin/mailman/listinfo/dev

Reply via email to