JSONP sends a JSON response with JSONP support. This method is identical to JSON, except that it opts-in to JSONP callback support. By default, the callback name is simply callback.
(data any, callback ...string)
| 558 | // This method is identical to JSON, except that it opts-in to JSONP callback support. |
| 559 | // By default, the callback name is simply callback. |
| 560 | func (r *DefaultRes) JSONP(data any, callback ...string) error { |
| 561 | raw, err := r.c.app.config.JSONEncoder(data) |
| 562 | if err != nil { |
| 563 | return err |
| 564 | } |
| 565 | |
| 566 | cb := "callback" |
| 567 | if len(callback) > 0 { |
| 568 | cb = callback[0] |
| 569 | } |
| 570 | |
| 571 | // Build JSONP response: callback(data); |
| 572 | // Use bytebufferpool to avoid string concatenation allocations |
| 573 | buf := bytebufferpool.Get() |
| 574 | buf.WriteString(cb) |
| 575 | buf.WriteByte('(') |
| 576 | buf.Write(raw) |
| 577 | buf.WriteString(");") |
| 578 | |
| 579 | r.setCanonical(HeaderXContentTypeOptions, "nosniff") |
| 580 | r.c.fasthttp.Response.Header.SetContentType(MIMETextJavaScriptCharsetUTF8) |
| 581 | // Use SetBody (not SetBodyRaw) to copy the bytes before returning buffer to pool |
| 582 | r.c.fasthttp.Response.SetBody(buf.Bytes()) |
| 583 | bytebufferpool.Put(buf) |
| 584 | return nil |
| 585 | } |
| 586 | |
| 587 | // XML converts any interface or string to XML. |
| 588 | // This method also sets the content header to application/xml; charset=utf-8. |
nothing calls this directly
no test coverage detected