I use angularJS $resource in my client and want to create a custom PATCH function, where I send data to my GO server. I want to parse the data on my GO server to a struct. I tried to send the data like the code below but the GO server outputs the values as '[object Object]' and generats an error when i try to marshal it. Should the data be included as a query string for PATCH or can it/should it be included in the request body?
var UpdateOneSchedule = $resource('/schedules/me/:bkchangeobject', {bkchangeobject:{}},{
update:{
method: 'PATCH',
isArray: false,
}
});
code snippet from my PATCH request
var updateObject = {"title":"value", "description":"value"}
console.log(updateObject)
UpdateOneSchedule.update({bkchangeobject:updateObject},
function(data){
//on success
},
function(httpResponse){
//on error
if(httpResponse.status === 409){ //StatusConflict
//
revertFunc()
}
});
The go server looks like this
func (db *bkDatabase) updateSchedule(w http.ResponseWriter, r *http.Request) {
bkDB := bkDatabase{db.session.Copy()}
defer bkDB.closeDB()
//check tokens
if bkSystem.db.isAuthorized(w, r) {
param := mux.Vars(r)["bkchangeobject"]
fmt.Println(param)
var change_object event
err := json.Unmarshal([]byte(param), &change_object)
if err != nil {
log.Fatalf("JSON Unmarshal error: %v", err)
}
fmt.Println(change_object)
} else {
}
}
I don't know how to use angularJS $resource, I'm new to angularJS. But I have achieved something similar, with the following codes:
In my controller I declare the object that will be sent to the server side:
$scope.message = {
From: {
Email: '',
Name: ''
},
Subject: '',
Content: ''
}
This object is linked to the scope, so it can be filled up by the user. I also created a service that will send the object to the server side:
.service('ContactService', function ($http) {
this.url = 'http://url.toyourwebservice.golang/what/you/want';
this.send = function (contact) {
return $http.post(this.url, contact);
};
});
This way, on the server side I can retrieve the object in the request body:
b, err := ioutil.ReadAll(r.Body)
if err != nil {
return err
}
if len(b) != 0 {
err = json.Unmarshal(b, contact)
if err != nil {
return err
}
// Do whatever you want with contact
}
Hope this help!