-1

I'm vague on the details of JSONP, which doesn't appear to be a proper specification at all.

I am writing a Perl client to communicate with a server that has a WebSocket JSONP API.

Do I need to use JavaScript to specify the JSONP callback, or can it be done in another way?

Borodin
  • 126,100
  • 9
  • 70
  • 144
  • 1
    http://stackoverflow.com/questions/2067472/what-is-jsonp-all-about - might be useful – samnu pel May 04 '17 at 14:01
  • @sanju: Thank you, but neither that nor [the Wikipedia page](https://en.wikipedia.org/wiki/JSONP) or anything else I've found on the web answer my question. – Borodin May 04 '17 at 14:06
  • 3
    "WebSocket JSONP" doesn't make much sense. The point of JSONP is that browsers allow GET requests to any URL. JSONP means that the server responds the the request with a small snippet of JavaScript, basically just a function call. The JSON data is passed as the parameter to the function. You reference the URL in a ` – Pointy May 04 '17 at 14:09
  • @Pointy: That's pretty much what I thought, but I blamed my lack of understanding. It's clearly what the documentation says, and what a couple of support guys have confirmed. Are you confident enough to post that as an answer? – Borodin May 04 '17 at 14:31
  • @Borodin well I don't know anything about the service you're wanting to use, so for all I know somebody concocted some weird "JSONP over WebSockets" solution, but I do know that the original idea (and most frequent continued purpose) was to allow access to dynamic services from client-side code across domain boundaries. – Pointy May 04 '17 at 14:37

2 Answers2

2

I believe it's possible to use JSONP API without JavaScript on client side provided the API follows "common JSONP contract". The latter means the server is expected to return JSON data wrapped into a function call, i.e. the server response (body) would look something like jsonpCallback({"key": "value"}).

To retrieve the JSON payload you just need to strip off the function wrapper:

$server_response =~ s/^\s*\w+\((.*)\);?\s*$/$1/s;   # now it's pure JSON: '{"key": "value"}'

N.B. In theory the function name may require much harder regex but again, if the "common contract" is in place, you define a simple callback function name in in your JSONP request.

Community
  • 1
  • 1
Dmitry Egorov
  • 9,542
  • 3
  • 22
  • 40
1

Yes, you need to use javascript. JSONP is mostly used to make cross origin ajax requests. And you need to specify a javascript function that will be called when your ajax request returns with response.

Mohit Bhardwaj
  • 9,650
  • 3
  • 37
  • 64
  • Wrong answer. This is about plain HTTP. The fact that the server response (hopefully) happens to be valid, non-malicious JavaScript is irrelevant, when you make the request from a different programming language. It's even safer because you don't have to blindly trust the remote server but can check whether the response meets your expectations. – Guido Flohr Sep 05 '18 at 16:39