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

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 unity terrain打包后地形错位,跟建筑不在同一个位置,怎么办
  • ¥15 FileNotFoundError 解决方案
  • ¥15 uniapp实现如下图的图表功能
  • ¥15 u-subsection如何修改相邻两个节点样式
  • ¥30 vs2010开发 WFP(windows filtering platform)
  • ¥15 服务端控制goose报文控制块的发布问题
  • ¥15 学习指导与未来导向啊
  • ¥15 求多普勒频移瞬时表达式
  • ¥15 如果要做一个老年人平板有哪些需求
  • ¥15 k8s生产配置推荐配置及部署方案