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

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 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?