简体   繁体   English

如何在golang中为youtrack rest api编写这样的curl请求?

[英]How to write such curl request in golang for youtrack rest api?

I am writing golang client with youtrack REST I have written the most most path of API.我正在用 youtrack REST 编写 golang 客户端,我已经编写了最多的 API 路径。 But faced problem with attaching files to an Issue.但是在将文件附加到问题时遇到了问题。 So, here there is small and good doc https://www.jetbrains.com/help/youtrack/devportal/api-usecase-attach-files.html所以,这里有一个小而好的文档https://www.jetbrains.com/help/youtrack/devportal/api-usecase-attach-files.html

The using the commands from this and other doc pages are worked with curl(via terminal) I am newby in golang, but have to write in this language.使用此文档页面和其他文档页面中的命令可与 curl(通过终端)一起使用,我是 golang 的新手,但必须用这种语言编写。

func createForm(form map[string]string) (string, io.Reader, error) {
    body := new(bytes.Buffer)
    mp := multipart.NewWriter(body)
    defer mp.Close()
    for key, val := range form {
        if strings.HasPrefix(val, "@") {
            val = val[1:]
            file, err := os.Open(val)
            if err != nil {
                return "", nil, err
            }
            defer file.Close()
            part, err := mp.CreateFormFile(key, val)
            if err != nil {
                return "", nil, err
            }
            io.Copy(part, file)
        } else {
            mp.WriteField(key, val)
        }
    }
    return mp.FormDataContentType(), body, nil
}

func AttachFileToIssue(path string, issueID string) {
    form := map[string]string{"image": "@image.jpeg", "key": "KEY"}
    _, body, err := createForm(form)
    if err != nil {
        panic(err)
    }

    req, err := http.NewRequest("POST", youTrackUrl+"/api/issues/"+issueID+"/attachments?fields=id,name", body)

    if err != nil {
        // handle err
    }
    req.Header.Set("Authorization", "Bearer "+youTrackToken)
    req.Header.Set("Content-Type", "multipart/form-data")

    resp, err := http.DefaultClient.Do(req)
    if err != nil {
        // handle err
    }
    fmt.Println(resp)
    if resp.StatusCode == http.StatusOK {
        bodyBytes, err := io.ReadAll(resp.Body)
        if err != nil {
            log.Fatal(err)
        }
        bodyString := string(bodyBytes)
        fmt.Println(bodyString)
    }
    defer func(Body io.ReadCloser) {
        err := Body.Close()
        if err != nil {

        }
    }(resp.Body)
}

The error code is:错误代码是:

{400 Bad Request 400 HTTP/1.1 1 1 map[Access-Control-Expose-Headers:[Location] Cache-Control:[no-cache, no-store, no-transform, must-revalidate] Content-Length:[110] Content-Type:[application/json;charset=utf-8] Date:[Sun, 10 Jul 2022 10:28:24 GMT] Referrer-Policy:[same-origin] Server:[YouTrack] X-Content-Type-Options:[nosniff] X-Frame-Options:[SAMEORIGIN] X-Xss-Protection:[1; mode=block]] 0xc0001cc100 110 [] false false map[] 0xc000254000 <nil>}

I used wireshark to check what is wrong, the problem is with mime-part, something missing.我用wireshark检查出了什么问题,问题出在mime-part上,有些东西不见了。

The curl REQUEST:卷曲请求:

curl -v -i -F upload=@/Users/jetbrains/Downloads/youtrack.txt \
-F upload=@/Users/jetbrains/Downloads/youtrack_new.jpeg \
-H 'Authorization: Bearer perm:cm9vdA==.MjZGZWI=.WB02vjX0cM2ltLTJXUE3VOWHpJYYNx' \
-H 'Content-Type: multipart/form-data' \
-X POST 'https://example.youtrack.cloud/api/issues/99-500/attachments?fields=id,name'

Ana Bartasheva is right!安娜·巴塔舍娃是对的!

The solution:解决方案:

func createForm(form map[string]string) (string, io.Reader, error) {
    body := new(bytes.Buffer)
    mp := multipart.NewWriter(body)
    defer func(mp *multipart.Writer) {
        err := mp.Close()
        if err != nil {

        }
    }(mp)
    var file *os.File
    for key, val := range form {
        if strings.HasPrefix(val, "@") {
            val = val[1:]
            file, err := os.Open(val)
            if err != nil {
                return "", nil, err
            }
            part, err := mp.CreateFormFile(key, val)
            if err != nil {
                return "", nil, err
            }
            _, err = io.Copy(part, file)
            if err != nil {
                return "", nil, err
            }
        } else {
            err := mp.WriteField(key, val)
            if err != nil {
                return "", nil, err
            }
        }
    }
    defer func(file *os.File) {
        err := file.Close()
        if err != nil {

        }
    }(file)

    return mp.FormDataContentType(), body, nil
}

func AttachFileToIssue(path string, issueID string) string {
    form := map[string]string{"path": "@" + path}
    mp, body, err := createForm(form)
    if err != nil {
        panic(err)
    }
    req, err := http.NewRequest("POST", youTrackUrl+"/api/issues/"+issueID+"/attachments?fields=id,name", body)

    if err != nil {
        // handle err
    }

    req.Header.Set("Authorization", "Bearer "+youTrackToken)
    req.Header.Set("Content-Type", mp)

    resp, err := http.DefaultClient.Do(req)
    if err != nil {
        // handle err
    }
    if resp.StatusCode == http.StatusOK {
        bodyBytes, err := io.ReadAll(resp.Body)
        if err != nil {
            log.Fatal(err)
        }
        bodyString := string(bodyBytes)
        return bodyString
    }
    defer func(Body io.ReadCloser) {
        err := Body.Close()
        if err != nil {

        }
    }(resp.Body)
    return ""
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM