Ticket #342 (closed enhancement: fixed)

Opened 4 years ago

Last modified 4 years ago

JSONP parameter in API

Reported by: dread Owned by: dread
Priority: awaiting triage Milestone:
Component: ckan Keywords:
Cc: Repository:
Theme:

Description (last modified by dread) (diff)

As a

CKAN client using JQuery

I want to

call the CKAN API and instead of receiving back JSON I get JSONP. i.e. "%s(%s)" % (callback, json_content)

Suggested implementation

All API calls allow the JSONP 'callback' parameter to be specified in the request and this wraps the JSON response. See suggested patch to rest.py by Donovan Hide:

http://knowledgeforge.net/ckan/trac/attachment/ticket/336/resource.patch

Test

import re import unittest

def test_jsonp_callback():

response = self.app.get('/api/search/resource/?url=http://www.scraperwiki.com&callback=jsoncallback') match = re.match('jsoncallback\(.*\);',response) self.assertTrue(match)

response = self.app.get('/api/search/resource/?url=http://www.scraperwiki.com') match = re.match('jsoncallback\(.*\);',response) self.assertFalse(match)

I think the point needs to be made that JSONP only works for GET requests and not POST/PUT/DELETE, so there needs to be a check for that in the _finish_ok method.

(thanks to Donovan Hide for test)

Change History

comment:1 Changed 4 years ago by dread

  • Description modified (diff)

comment:2 Changed 4 years ago by dread

  • Status changed from new to closed
  • Resolution set to fixed

Done in cset:61f145b7d4a8

Note: See TracTickets for help on using tickets.