// 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))) } }
// Get stream as string // // @param firstLine string // @return string // @protected func (this *Stream) toString(firstLine string) string { str := firstLine // add headers if this.Headers != nil { for key, value := range this.Headers { if key == "0" { // response only continue } if value != nil { // remove? str += util.StringFormat("%s: %s\r\n", key, value) } } } // add headers/body seperator str += "\r\n" // add body if this.Body != nil { switch this.Body.(type) { case string: str += this.Body.(string) default: body, err := util.UnparseBody(this.Body) if err == nil { str += body } } } return str }
// Get attachment data as JSON string. // // @param encode bool // @return string func (this *DocumentAttachment) ToJson(encode bool) string { json, _ := util.UnparseBody(this.ToArray(encode)) return json }