jsonp

package module
v0.0.0-...-ddb016d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 21, 2016 License: MIT Imports: 3 Imported by: 1

README

jsonp A small library for allowing JSONp requests

Go (golang) routines for supporting JSONp.

The primary interface is:


	func JsonP(s string, res http.ResponseWriter, req *http.Request) string 

This allows converting a return string in JSON to a JSONp callback. If the parameter on the url "callback" is supplied then the returned string is wrapped in the callback name.

An example handler using this is:


	func handleVersion(res http.ResponseWriter, req *http.Request) {
		res.Header().Set("Content-Type", "application/json")
		io.WriteString(res, jsonp.JsonP(fmt.Sprintf(`{"status":"success", "version":"1.0.0"}`+"\n"), res, req))
	}

If you already have the parameters parsed then you can pass the value for callback to JsonP_Param and save parsing the URL again.


	func handleVersion(res http.ResponseWriter, req *http.Request) {
		res.Header().Set("Content-Type", "application/json")
		callback := r.URL.Query()["callback"][0]; 
		io.WriteString(res, jsonp.JsonP_Param(fmt.Sprintf(`{"status":"success", "version":"1.0.0"}`+"\n",callback), res, req))
	}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var JSON_Prefix string = ""

Functions

func JsonP

func JsonP(s string, res http.ResponseWriter, req *http.Request) string

Take a string 's' in JSON and if a get parameter "callback" is specified then format this for JSONP callback. If it is not a JSONp call (no "callback" parameter) then add JSON_Prefix to the beginning.

func JsonP_Param

func JsonP_Param(s string, res http.ResponseWriter, callback string) string

If "callback" is not "" then convert the JSON string 's' to a JSONp callback. If it is not a JSONp call (no "callback" parameter) then add JSON_Prefix to the beginning.

func PrependPrefix

func PrependPrefix(s string) string

For non-JSONP callable - just prepend the prefix and return.

func SetJsonPrefix

func SetJsonPrefix(p string)

Set a prefix that will be pre-pended to every return JSON string.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL