Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

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

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.

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

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

like image 812
Prashant Avatar asked Aug 27 '15 17:08

Prashant


People also ask

How do you send a PATCH request in Golang?

As you can see above, we start by doing: 1) defining the JSON payload that we want to send to the endpoint we are calling. 2) Instantiate the http Client . 3) Define the request and set any additional headers. 4) Perform the actual request.

What is HTTP PATCH request?

The HTTP PATCH request method applies partial modifications to a resource. PATCH is somewhat analogous to the "update" concept found in CRUD (in general, HTTP is different than CRUD, and the two should not be confused). A PATCH request is considered a set of instructions on how to modify a resource.


2 Answers

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(`[{"Type": "Set", "Name": "title", "Value": "Buy cheese and bread for breakfast."}]`)
like image 157
dev-null-dweller Avatar answered Oct 01 '22 10:10

dev-null-dweller


PATCH and POST are different http verbs.

I think you just need to change this;

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

to

 req, _ := http.NewRequest("PATCH", 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.

like image 37
evanmcdonnal Avatar answered Oct 01 '22 09:10

evanmcdonnal