5

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

有人可以指出我在上面的代码中缺少什么吗?

4

2 回答 2

7

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

要更改title属性,它将如下所示:

var jsonStr = []byte(`[{"Type": "Set", "Name": "title", "Value": "Buy cheese and bread for breakfast."}]`)
于 2015-08-27T18:21:38.927 回答
2

PATCH并且POST是不同的http动词。

我认为你只需要改变它;

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

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

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

于 2015-08-27T17:22:50.997 回答