douju4594
2018-12-01 10:52
浏览 918
已采纳

golang http.Post请求返回响应404

I have written a go code to create a issue in a project in my github reposiory. I am using parameters as mentioned here [https://developer.github.com/v3/issues/#create-an-issue][1]

But I am getting response with status 404. Below is my code.

package main

import (
    "bytes"
    "encoding/json"
    "fmt"
    "net/http"
)

func main() {
    param := map[string]string{"title": "issue1", "body": "aassddrff", "assignee": "vigneshkm"}

    query, _ := json.Marshal(param)
    queryUrl := "https://api.github.com/repos/vigneshkm/first_repo/issues"

    fmt.Println("query:", string(query))
    resp, err := http.Post(queryUrl, "application/json", bytes.NewBuffer(query))

    fmt.Println("query_status : ", resp.StatusCode, "err : ", err)
    resp.Body.Close()
}

I am a beginner to web programming and I am not able to understand my mistake in this code. Kindly help me :)

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

我在github仓库中写了一个go代码来在项目中创建问题。 我正在使用此处提到的参数[ https:/ /developer.github.com/v3/issues/#create-an-issue][1]

但是我得到的状态为404。下面是我的代码。

 包main 
 
import(
“字节” 
“ encoding / json” 
“ fmt” 
“ net / http” 
)
  
func main(){
参数:= map [string] string {“ title”:“ issue1”,“ body”:“ aassddrff”,“ assignee”:“ vigneshkm”}} 
 
查询,_:=  json.Marshal(param)
 queryUrl:=“ https://api.github.com/repos/vigneshkm/first_repo/issues"

 fmt.Println(” query:“,string(query))
  resp,err:= http.Post(queryUrl,“ application / json”,bytes.NewBuffer(query))
 
 fmt.Println(“ query_status:”,resp.StatusCode,“ err:”,err)
  resp.Body.Close()
} 
   
 
 

我是Web编程的初学者,但无法理解此代码中的错误。 请帮助我:)

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongmo3413 2018-12-01 15:03
    已采纳

    1) Access to https://github.com/settings/tokens
    2) Click "Generate new token"
    3) Copy the token.
    4) Paste the token to "YOUR_ACCESS_TOKEN_HERE" in below. (I mean, remove "YOUR_ACCESS_TOKEN_HERE" and paste your token there).

    $ cat main.go 
    package main
    
    import (
        "bytes"
        "encoding/json"
        "fmt"
        "net/http"
    )
    
    func main() {
        param := map[string]string{"title": "issue1", "body": "aassddrff", "assignee": "vigneshkm"}
    
        query, _ := json.Marshal(param)
        queryUrl := "https://api.github.com/repos/vigneshkm/first_repo/issues?access_token=YOUR_ACCESS_TOKEN_HERE"
    
        fmt.Println("query:", string(query))
        resp, err := http.Post(queryUrl, "application/json", bytes.NewBuffer(query))
    
        fmt.Println("query_status : ", resp.StatusCode, "err : ", err)
        resp.Body.Close()
    }
    

    This is the result in my environment.

    $ go run main.go 
    query: {"assignee":"vigneshkm","body":"aassddrff","title":"issue1"}
    query_status :  201 err :  <nil>
    

    https://github.com/vigneshkm/first_repo/issues/2

    点赞 评论
  • duanrongshi1544 2018-12-01 12:10

    You're missing access token while request.

    queryUrl := "https://api.github.com/repos/vigneshkm/first_repo/issues?access_token=" + OAUTH_TOKEN
    

    Check this document

    点赞 评论

相关推荐 更多相似问题