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

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

图片转代码服务由CSDN问答提供 功能建议

BitBucket中是否有任何REST API,可以从GoLang中调用它,以便它将创建一个新的REPOSITORY。 我可以获取现有的详细信息,但无法创建新的详细信息。 请记住,CURL不是必需的。 请随时帮忙。 还有通过JAVA进行的任何方法吗? 如果Java可以做到,那么我认为GoLang应该可以。 建议!

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

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条)

相关推荐 更多相似问题