I have an app in google engine with a go backend. I'm trying to retrieve a json file that I previously saved in google cloud storage. The backend is based on polymer and javascript. The problem is that the data need to be retrieved using the user-id through a core-ajax call.
Here is the javascript code I'm using at the moment:
loadTrials : function loadTrials() {
var _this = this,
load = this.shadowRoot.querySelector('#load-trial');
load.url="http://url/loadTrials";
load.go();
load.addEventListener('core-response', function(ev) {
var json = ev.detail.response;
_this.trialData = JSON.parse(json)['trial-data'];
console.log(JSON.parse(json)['trial-data'])
}, false);
}
The HTML
<core-ajax id="load-trial" method="GET"></core-ajax>
And the backend in go(just the relevant part):
func handleloadTrials(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
//retrieve trials
http.ServeFile(w, r, JSON file related to user id)
}
I know this code is wrong, but I'm completely stuck and I don't know how to pass the user ID to the GET method, and then use the ID to retrieve the specific file on cloud storage (it's a json file) and then pass this Json file to the http.ServeFile method in Go.
Any help? Thanks a lot.
I partially solved in this way
Javascript:
loadTrials : function loadTrials() {
var _this = this,
load = this.shadowRoot.querySelector('#load-trial');
//pass userID to core-ajax
_this.userID = _this.app.userSession.user;
load.url="http://url/loadTrials";
load.go();
load.addEventListener('core-response', function(ev) {
var json = ev.detail.response;
_this.trialData = JSON.parse(json)['trial-data'];
console.log(JSON.parse(json)['trial-data'])
}, false);
}
HTML:
<core-ajax id="load-trial" params="{{userID}}" method="GET"></core-ajax>
Go:
type trialStruct []struct {
Trial struct {
Index int `json:"index"`
Word string `json:"word"`
WordDisplayTime int `json:"wordDisplayTime"`
AnswerMaxTime int `json:"answerMaxTime"`
FixationTime int `json:"fixationTime"`
Train bool `json:"train"`
Test bool `json:"test"`
Grammatical bool `json:"grammatical"`
Grammar string `json:"grammar"`
keyboard bool `json:"keyboard"`
}
Metadata struct {
}
}
func (d *saveData) readFile(fileName string) trialStruct {
trialName := fileName + "/trials"
rc, err := storage.NewReader(d.ctx, bucket, trialName)
if err != nil {
d.errorf("readFile: unable to open file from bucket %q, file %q: %v", bucket, trialName, err)
}
defer rc.Close()
slurp, err := ioutil.ReadAll(rc)
if err != nil {
d.errorf("readFile: unable to read data from bucket %q, file %q: %v", bucket, fileName, err)
}
var trialJson trialStruct
err1 := json.Unmarshal(slurp, &trialJson)
if err != nil {
log.Printf("error:", err1)
}
return trialJson
}
But now I have the Encoded Json file which I'm not sure how to send back to the javascript, any help? Thanks
<core-ajax url="{{url}}" handleAs="json" params='{"userID": "{{userID}}"}'></core-ajax>
The parameter should be on JSON format.