douju4594 2018-12-01 10:52 采纳率: 100%
浏览 1704
已采纳

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

  • 写回答

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

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

报告相同问题?

悬赏问题

  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺