// Get query data as string. // // @return string func (this *Query) ToString() string { if this.DataString != "" { return this.DataString } for key, value := range this.Data { if util.TypeReal(value) == "[]string" { value = _fmt.Sprintf("[\"%s\"]", _str.Join(value.([]string), "\",\"")) } this.DataString += _fmt.Sprintf( "%s=%s&", util.UrlEncode(key), util.UrlEncode(util.String(value))) } if this.DataString != "" { // drop last "&" this.DataString = this.DataString[0 : len(this.DataString)-1] // purify some encoded stuff this.DataString = _str.NewReplacer( "%5B", "[", "%5D", "]", "%2C", ",", ).Replace(this.DataString) } return this.DataString }
// Set body. // // @param body interface{} // @return void // @panics // @implemented func (this *Request) SetBody(body interface{}) { if body != nil && // these methods not allowed for body this.Method != METHOD_HEAD && this.Method != METHOD_GET { switch body := body.(type) { case string: // @overwrite if this.GetHeader("Content-Type") == "application/json" { // embrace with quotes for valid JSON body body = util.Quote(body) } this.Body = body default: bodyType := _fmt.Sprintf("%T", body) if util.StringSearch(bodyType, "^u?int(\\d+)?|float(32|64)$") { // @overwrite this.Body = util.String(body) } else { if this.GetHeader("Content-Type") == "application/json" { // @overwrite body, err := util.UnparseBody(body) if err != nil { panic(err) } this.Body = body } } } // auto-set content length headers this.SetHeader("Content-Length", len(this.Body.(string))) } }
// Read file. // // @return void // @panics func (this *DocumentAttachment) ReadFile(encode bool) { if this.File == "" { panic("Attachment file is empty!") } // get file info info, err := util.FileInfo(this.File) if err != nil { panic(err) } this.ContentType = util.String(info["mime"]) // get file contents data, err := util.FileGetContents(this.File) if err != nil { panic(err) } this.Data = data // convert to base64 if encode=true if encode { this.Data = util.Base64Encode(data) } this.DataLength = int64(len(data)) }
// Set header. // // @param key string // @param value interface{} // @return void // @panic func (this *Stream) SetHeader(key string, value interface{}) { switch value.(type) { case nil: // so nil means remove delete(this.Headers, key) case int, bool, string: this.Headers[key] = util.String(value) default: panic("Unsupported value type '" + _fmt.Sprintf("%T", value) + "' given!") } }
// Set body. // // @param body interface{} // @return void // @implemented func (this *Response) SetBody(body interface{}) { if body != nil { this.Body = util.String(body) } }