8

I am using WebApiContrib.Formatting.Jsonp within a Web API 2 service, plus Swagger for API documentation and testing.

When I trigger any JSONP method through Swagger, my service crashes in file JsonpMediaTypeFormatter.cs on the following line:

throw new InvalidOperationException(Properties.Resources.NoCallback);
// NoCallback = The name 'NoCallback' does not exist in the current context

For one thing, I don't understand why Swagger doesn't allow specifying a callback name for JSONP requests. But more importantly, I don't want the service to crash because of it.

Questions:

  • How can we handle such errors on the service side?
  • Is there a way to make Swagger send JSONP requests properly? (like pre-defining a callback name for all JSONP requests?)
Community
  • 1
  • 1
vitaly-t
  • 24,279
  • 15
  • 116
  • 138
  • 1
    Use fiddler http://www.telerik.com/fiddler or something similar to make sure your jsonp requests are well-formed. Is there any reason for you to use JSONP and not plain JSON other than CORS? – Stack Aug 12 '16 at 14:30
  • Telerik products for .NET are all commercial, I can't use it for that reason, plus I suspect it would be an overkill for the type of question i'm asking. And I need JSONP to handle requests among many servers, so yes, CORS is the only reason I'm using it. – vitaly-t Aug 12 '16 at 14:47
  • 2
    Its just a free debugging proxy. It's a free standalone desktop application having nothing to do with .NET. All it does is capture your HTTP requests/responses for you to debug. You can try something like https://www.getpostman.com/ which is pretty but not as flexible as Fiddler. – Stack Aug 12 '16 at 15:08
  • It is much easier to properly configure CORS in WebAPI App and work with JSON. I can give you some pointers if you are interested. – Stack Aug 12 '16 at 15:13
  • @StackExchanger unfortunately, I've killed way too much time trying to make CORS work. None of the posts on StackOverflow helped me, and I've read and tried many of them. – vitaly-t Aug 19 '16 at 10:09
  • let me help you with proper CORS. I keep insisting on it just because I once went JSONP route myself and its a mess. Do you use OWIN in your app? – Stack Aug 19 '16 at 10:24
  • 3
    [Swagger doesn't support JSONP](https://github.com/OAI/OpenAPI-Specification/issues/444), and you can detect the missing callback parameter in the query string by reading the query string. Does that answer your questions? – CodeCaster Aug 22 '16 at 10:56
  • @CodeCaster possibly, if you show where exactly should I do the verification, to make sure it doesn't trigger the error. Do you want to make it into an answer? – vitaly-t Aug 22 '16 at 11:12
  • In WebApi, its really simple to make it CORS enabled. I would suggest you to enable CORS rather going with JSONP. In your WebApi project, install NuGet package - Microsoft.AspNet.WebApi.Cors and in api config add config.EnableCors(new EnableCorsAttribute("*", "*", "*")); – Developer Aug 24 '16 at 06:29
  • @Developer your suggestion is what one finds after 1 minute of google search. I've spent days trying to make it work, without success. – vitaly-t Aug 24 '16 at 10:51
  • Not mere google search, I have implemented this in atleast 10 projects. The consumers included normal jQuery.ajax, anugularJs project, windows UWP etc – Developer Aug 24 '16 at 10:52
  • @vitaly-t - Do you mind telling about your client side technology or how you are trying to consume this REST service? – Developer Aug 24 '16 at 10:57
  • @vitaly-t - well there is a difference between cors handling in owin vs non-owin (system.web) scenario; also the suggested fiddler (or any tool that can fire crafted OPTIONS request) helps a lot - I suggest you open a question describing your effort to setup CORS policy - I'll be glad to answer that one - jsonp is being deprecated on purpose in many frameworks – Ondrej Svejdar Aug 30 '16 at 08:04

1 Answers1

1
PM> Install-Package Newtonsoft.Json.Schema

Or you can down load the source at https://github.com/JamesNK/Newtonsoft.Json.Schema/releases

JSchema schema = JSchema.Parse(request.Schema);
JToken json = JToken.Parse(request.Json);

// validate json
IList<ValidationError> errors;
bool valid = json.IsValid(schema, out errors);

// return error messages and line info to the browser
return new ValidateResponse
{
    Valid = valid,
    Errors = errors
};
Bart
  • 9,925
  • 7
  • 47
  • 64
Balan
  • 421
  • 6
  • 12