How to pass along Json without parsing in web api? - asp.net-mvc

I have a web api based server. All controller methods accept objects and return objects (parsed from/to json by the framework). This has been working fine.
However, due to a new requirement, I need to create an adapter and place it between clients and the server. The adapter will be responsible for translation of a small subset of client messages before passing them to the server.
I'd like to avoid unnecessary deserialization/serialization from/to json of messages that do not need to be translated. That is, in majority of cases I'd like to pass raw json client requests to the server and raw json server replies to clients.
The controller methods that will do the translation will have 'standard' signatures - no problem here. However, I'm not sure how to forward messages (json strings) from clients to the server (and vice versa) without deserialization and serialization.
Is this possible in ApiController?
Thanks.

Related

Is there a way to use Swagger just for validation without using the whole framework?

Suppose I have an existing Java service implementing a JSON HTTP API, and I want to add a Swagger schema and automatically validate requests and responses against it without retooling the service to use the Swagger framework / code generation. Is there anything providing a Java API that I can tie into and pass info about the requests / responses to validate?
(Just using a JSON schema validator would mean manually implementing a lot of the additional features in Swagger.)
I don't think there's anything ready to do this alone, but you can easily do this by the following:
Grab the SchemaValidator from the Swagger Inflector project. You can use this to validate inbound and outbound payloads
Assign a schema portion to your request/response definitions. That means you'll need to assign a specific section of the JSON schema to your operations
Create a filter for your API to grab the payloads and use the schema
That will let you easily see if the payloads match the expected structure.
Of course, this is all done for you automatically with Inflector but there should be enough of the raw components to help you do this inside your own implementation

Export breeze entities in the server side to json

I am looking for a way to export breeze entities on the server side to a json string which a breezejs manager can import from the client side. I looked all over the breeze APIs (both public and internal source code) but I couldn't find an obvious way of achieving this. There is a possibility of getting the desired results by using BreezeSharp (a .NET breeze client) on the server side but I would like to see if this is achievable with using the breeze server APIs only.
First you need to determine the shape of the bundle to be imported, i.e. something that manager.importEntities will understand. I don't think the format is documented, but you can reverse-engineer it by using:
var exported = manager.exportEntities(['Customer', 'Product'], {asString:true, includeMetadata:false});
Then pretty-print the value of exported to see the data format. See EntityManager.exportEntities for more info.
Once you have that, you can re-create it on the server. In C#, you can build it up using Dictionary and List objects, then serialize it using Json.NET.
An alternative approach would be to have your webhook just tell the client to initiate a query to retrieve the data from the server.

How to stream JSON response from MVC controller method?

My company has a Phonegap application that's running into errors with UIWebView; it's hitting the 10MB memory cap. We're using .NET MVC4 as our backend.
This is due to a very large JSON response from a sync call. The JSON string (approximately 12 megs of data) is saved to memory, which causes a memory exception. Due to the way our application works, we have to send this data.
I'm considering using Oboe.JS to stream in the JSON response (to avoid the allocation of the full JSON object). I think that this may fix the issue on the frontend, but I'd like to stream the JSON response from the backend. I know that there are chunking methods, but I'd prefer not to use that option.
Currently, our controller method produces an object, serializes it to JSON, and sends the entire response. I would like to open a one-way HTTP stream, if possible, and asynchronously write JSON data to this stream (which Oboe would parse on the client side).
Are there any technologies that fit my use case, or can someone point me to some methods I may use to accomplish this? I don't need a two-way conduit - I just want to write the JSON to the HTTPstream as objects are produced.
Thank you.

Use of REST with scaffolding actions

I see that Grails 2.3 is using REST for the CRUD actions in scaffolding. While it's a great way to learn how REST works, I am wondering if using REST to communicate inside of a single application stack is very efficient. Doesn't it send the request all the way up to the network layer and back down again instead of going directly from the app server to the database? I am visualizing a "pop fly" as opposed to a "line drive." Am I misunderstanding how this works?
I assume when you say, "using REST for the CRUD actions in scaffolding" you are referring to the basic scaffolding (i.e. generate-all example.Book). The scaffolded controllers are not calling the REST API (https://yourapp/book.json) to retrieve the data, they are still using GORM to access the database, and then using the Respond method to render the data in the appropriate format based on the Request's content-type (XML, JSON, HTML). So,
Typical request-response cycle
Client, typically an HTML page, makes request (GET http://yourapp/books/1)
Grails 'parses' request params (id: 1)
Grails' GORM retrieves data from database and creates object instance
Grails resolves response content type to HTML
Grails "responds" to the request with an HTML response using a view from the views directory
SPA/API call
Client, typically javascript, makes request (GET http://yourapp/books/1.json **)
Grails 'parses' request params (id: 1)
Grails' GORM retrieves data from database and creates object instance
Grails resolves response content type to JSON
Grails "responds" to the request with an JSON response
Client consumes the JSON response and acts accordingly
** content-type can be specified in a number of ways, just used the .json suffix since it is the most transparent. See http://grails.org/doc/2.3.x/guide/single.html#contentNegotiation.
And to answer your question regarding whether it "very efficient". I would argue that it is almost always yes, because your payload tends to be much smaller, since you are only transferring data, not data + formatting (HTML, javascript, css, etc). It also provides a way of separating concerns, allowing the client to focus on state and presentation and the backend to focus on data. Furthermore, it means that you can create multiple clients (mobile, desktop-based, web-based) using the same backend API.

Posting JSON data to Web API - where do I even start?

I have zero experience with Web API and JSON. My requirements are to create a service (I'm using Web API for this) that will accept MIME Encoded JSON data. The service will take that data, insert it into a database, and return the primary key value back to the client.
My hang-up is being able to know where to even start with this. A couple of questions that I have are:
When the device sends the JSON data, how will the service "accept" it? Meaning, what's being passed to the service isn't an URL that we commonly see with MVC (/Controller/Action/ID) which then invokes the Action Method. So, how will the service know what to invoke if I'm passing raw JSON data?
How would I test this if I don't have a device that sends the JSON data yet? Would I manually invoke an AJAX call and call that particular action method and pass in the JSON data that way?
I apologize for the seemingly elementary questions.
Thanks.
When you call a WebAPI-method you still have to specify the endpoint:
Example:
PUT /api/people
MVC knows from that that it should call the put-method on the PeopleController.
You can send raw JSON-data to test it. A good tool for that is HttpFiddler: http://fiddler2.com/
As for where to start, try to create a basic WebAPI-project with visual studio, it will include some samples and you can get going from that. If you run into wall, you can come back here

Resources