MCPcopy
hub / github.com/gofiber/fiber / JSONP

Method JSONP

res.go:560–585  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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.
560func (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.

Callers

nothing calls this directly

Calls 7

setCanonicalMethod · 0.95
GetMethod · 0.65
WriteStringMethod · 0.65
WriteByteMethod · 0.65
WriteMethod · 0.65
BytesMethod · 0.65
PutMethod · 0.65

Tested by

no test coverage detected