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

huangapple go评论77阅读模式
英文:

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))

我不明白为什么它不起作用?我得到了以下响应体,这不是我期望的。我期望得到一个成功的响应。

<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>
</html>

请问有人可以指出我在上述代码中漏掉了什么吗?

英文:

I have written the following code to add a title field to the document 1 in my raven database.

url := &quot;http://localhost:8083/databases/drone/docs/1&quot;
fmt.Println(&quot;URL:&gt;&quot;, url)

var jsonStr = []byte(`{&quot;title&quot;:&quot;Buy cheese and bread for breakfast.&quot;}`)
req, _ := http.NewRequest(&quot;PATCH&quot;, url, bytes.NewBuffer(jsonStr))
req.Header.Set(&quot;X-Custom-Header&quot;, &quot;myvalue&quot;)
req.Header.Set(&quot;Content-Type&quot;, &quot;application/json&quot;)

client := &amp;http.Client{}
resp, err := client.Do(req)
if err != nil {
	panic(err)
}
defer resp.Body.Close()

body, _ := ioutil.ReadAll(resp.Body)
fmt.Println(&quot;response Body:&quot;, string(body))

I don't understand why it is not working? I am getting the following response Body which is not what I am expecting. I am expecting a success response.

&lt;html&gt;
&lt;body&gt;
	&lt;h1&gt;Could not figure out what to do&lt;/h1&gt;
	&lt;p&gt;Your request didn&#39;t match anything that Raven knows to do, sorry...&lt;/p&gt;
&lt;/body&gt;

</html>

Can someone please point out what am I missing in the above code?

答案1

得分: 7

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

要更改 title 属性,代码如下:

var jsonStr = []byte(`[{&quot;Type&quot;: &quot;Set&quot;, &quot;Name&quot;: &quot;title&quot;, &quot;Value&quot;: &quot;买奶酪和面包作为早餐。&quot;}]`)
英文:

For PATCH request, you need to pass an array with patch commands (in json format) to execute.

To change the title attribute, it will look like:

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

答案2

得分: 3

PATCHPOST是不同的HTTP动词。

我认为你只需要将这段代码中的:

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

改为:

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

或者至少这是第一步。根据评论,我猜测你的请求体也有问题。

英文:

PATCH and POST are different http verbs.

I think you just need to change this;

 req, _ := http.NewRequest(&quot;POST&quot;, url, bytes.NewBuffer(jsonStr))

to

 req, _ := http.NewRequest(&quot;PATCH&quot;, url, bytes.NewBuffer(jsonStr))

Or at least that is the first thing. Based on comments I would speculate the body of your request is also bad.

huangapple
  • 本文由 发表于 2015年8月28日 01:14:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/32255599.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定