douhongxie5436 2018-01-18 03:07 采纳率: 100%
浏览 129
已采纳

如何使用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 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!!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 我这模型写的不对吗?为什么lingo解出来的下面影子价格这一溜少一个变量
  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波