Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Golang Reference issue
func (self *Client) Store(bucket string, returnBody bool, data *Data) (*Data, error) {
// check if the key exists for conditional put/post
path := ""
returnData := Data{}
resp := http.Response{}
// check if the key exists
if data.value == "" {
return &returnData, errors.New("RGO: no value defined for the key")
}
if data.key != "" {
// put
path = fmt.Sprintf("/buckets/%s/keys/%s",bucket,data.key)
if returnBody {
path = path + "?returnbody=true"
}
values := url.Values{data.key:{data.value}}
err := self.query("PUT",path,values,&resp)
if err != nil {
return &returnData, err
}
} else {
//post
path = fmt.Sprintf("/buckets/%s/keys",bucket)
if returnBody {
path = path + "?returnbody=true"
}
values := url.Values{"":{data.value}}
err := self.query("POST",path,values,&resp)
if err != nil {
return &returnData, err
}
}
func (self *Client) query(method string, path string, values url.Values, r *http.Response) (error) {
// construct the base URL
riakurl := fmt.Sprintf("%s:%d",self.Address,self.Port)
endpoint, err := url.Parse(riakurl)
if err != nil {
return err
}
endpoint.Path = path
if method == "GET" {
endpoint.RawQuery = values.Encode()
}
var body io.Reader
if method != "GET" && values != nil {
body = strings.NewReader(values.Encode())
}
if self.Log {
fmt.Println("R before mod : ", r)
fmt.Println("RGO :", method, endpoint.String())
fmt.Println(values.Encode())
}
request, err := http.NewRequest(method, endpoint.String(), body)
if err != nil {
return err
}
response, err := http.DefaultClient.Do(request)
r = response
if self.Log {
fmt.Println("RESPONSE : ", response)
fmt.Println("R : ", r)
}
@RadioactiveMouse

This comment has been minimized.

Copy link
Owner Author

RadioactiveMouse commented Mar 3, 2013

Basically once I get the response at the bottom I try to set "r" so that I can use it as "resp" but it remains an empty response despite the print statements to the contrary at the bottom which print fine.

@jessta

This comment has been minimized.

Copy link

jessta commented Mar 3, 2013

Line 63.
You want:
*r = *response

Since you want to copy the value from the location pointed to by the return from Do() to the location pointed by r.

@RadioactiveMouse

This comment has been minimized.

Copy link
Owner Author

RadioactiveMouse commented Mar 3, 2013

Thanks, that was it. Must have tried every other permutation but that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.