douhongxie5436
2018-01-18 03:07 浏览 70
已采纳

如何使用GOLang通过调用REST API在BitBucket中创建REPOSITORY

Is there any REST API from BitBucket, which can be called from a GoLang so that it will create a new REPOSITORY. I can fetch the details of existing but not able to create a new one. Remember CURL is not requirement. Kindly help, stuck from some time into it. Is there any way do it via JAVA as well? If Java can do, then I think GoLang should be able to. Suggest!

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答 默认 最新

  • 已采纳
    douzheyo2617 douzheyo2617 2018-01-23 08:14

    Thanks for Help guys! Yes, I am able to resolve this issue,with a colleague pointing out the mistake. Things required: 1. You should be having complete access for the bitBucket. 2. You should have correct URL where to connect with for the REST API. Note: REST API url is different than that of direct URL and get the versions Correct.

    Go Code for same is::

    import (
        "encoding/json"
        "bytes"
        "fmt"
        "io/ioutil"
        "net/http"
    )
    
    func main() {
    
        url:=fmt.Sprintf("https://<Server BitBucket>/rest/api/1.0/projects/<PROJECT WHERE REPO TO BE CREATED>/repos");
       jsonData := map[string]string{"name":"<REPONAME>","scmID":"git","forkable":"true"}  
        jsonValue,_:=json.Marshal(jsonData)
        req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonValue))
        req.Header.Set("Content-Type", "application/json")
            req.SetBasicAuth("<USERNAME>", "<PASSWORD>")
        fmt.Println("++",req)
    
        client := &http.Client{}
        resp, err := client.Do(req)
        if err != nil {
            panic(err)
        }
    
        defer resp.Body.Close()
    
        fmt.Println("response Status:", resp.Status)
        fmt.Println("response Headers:", resp.Header)
        body, _ := ioutil.ReadAll(resp.Body)
        fmt.Println("response Body:", string(body))
        }
    

    This will give the response as 201 and yes, it will be created!!

    点赞 评论 复制链接分享
  • duanmeng1858 duanmeng1858 2018-01-18 09:08

    Looking through their documentation I found this endpoint which allows you to create repos using their API.

    Calling a REST API endpoint can be done from any language.

    Here is a nice tutorial where it explains how you can call json API endpoints using GO.

    点赞 评论 复制链接分享

相关推荐