如何使用 golang 向 raven db 服务器发出 HTTP 补丁请求?

How to make HTTP Patch request to raven db server using golang?

我已经编写了以下代码来向我的 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))

我不明白为什么它不起作用?我收到以下响应 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>

有人可以指出我在上面的代码中遗漏了什么吗?

PATCHPOST 是不同的 http 动词。

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

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

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

或者至少这是第一件事。根据评论,我推测您的请求正文也有问题。

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

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

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