如何使用golang向raven数据库服务器发出HTTP补丁请求



我编写了下面的代码来为raven数据库中的文档1添加标题字段。

url := "http://localhost:8083/databases/drone/docs/1"
fmt.Println("URL:>", url)
var jsonStr = []byte(`{"title":"Buy cheese and bread for breakfast."}`)
req, _ := http.NewRequest("PATCH", url, bytes.NewBuffer(jsonStr))
req.Header.Set("X-Custom-Header", "myvalue")
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
    panic(err)
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
fmt.Println("response Body:", string(body))

我不明白为什么它不工作?我得到以下的响应体,这不是我所期待的。我期待一个成功的回应。

<html>
<body>
    <h1>Could not figure out what to do</h1>
    <p>Your request didn't match anything that Raven knows to do, sorry...</p>
</body>

有人能指出我在上面的代码缺少什么?

对于PATCH请求,您需要传递一个包含补丁命令(json格式)的数组来执行。

要更改title属性,它看起来像:

var jsonStr = []byte(`[{"Type": "Set", "Name": "title", "Value": "Buy cheese and bread for breakfast."}]`)

PATCHPOST是不同的http动词

我认为你只需要改变这个;

 req, _ := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))

 req, _ := http.NewRequest("PATCH", url, bytes.NewBuffer(jsonStr))

或者至少这是第一件事。根据评论,我推测你的请求正文也很糟糕。

最新更新