Is it possible to POST "url encoded" parameters to a remote web service instead of JSON or XML ?
My rails application consumes a web service which takes URL encoded parameters (content-type: application/x-www-form-urlencoded) in POST requests and give JSON answers.
Is this kind of RESTful services common ?
When you make a hit to a JSON or XML web service using Ajax then the parameters are just getting encoded as either GET or POST, and is typically sent using the application/x-www-form-urlencoded content type anyway (see http://api.jquery.com/jQuery.ajax/ for an example specific to jQuery).
So, basically, yes, it is possible to send data in any format (JSON, XML, BSON etc.) in this manner.
Related
I recently picked up Nim and am in the process of re-implementing an existing web-application of mine to get some experience in the language.
This web-application used JWT for authentication, with the typical split into an access-token and a refesh-token.
The old way my application did refresh, was by receiving the refresh token via a POST request. The request body of that POST request would just be a raw JSON string and my application would grab the string off of that body and do its magic. The string would look like this:
{"refresh":"<JWT TOKEN STRING>"}
I've run into an issue when I wanted to access that raw JSON string in Prologue. There doesn't seem to be a way to do this.
When looking at the context's request, neither the PostParams nor the FormParams contain anything, they're empty. I can't find anything in the documentation about JSON-request bodies either and nothing in the source code looks like it is what I would want.
Is there no way for me to access the raw request body? Am I forced to change the way I send my refresh token?
After some more skillfull searching through the documentation I stumbled upon the answer I desired. There is a body() proc that allows you to access the raw HTTP body.
I'm trying to use RestClient and Faraday to query an endpoint which returns multiple files in a multipart response. How do I parse the multipart envelopes in the response body? Rack::Utils::Multipart.parse_multipart would have done it, but in my case, this is outside of Rack. I'm open to using a different HTTP client if its helps.
Almost none of the popular HTTP clients, in almost any language, handle multipart responses from a server. In fact I'd be surprised if you can easily find HTTP servers with baked in multipart response capabilities. It's just not a common use case.
You'll find the converse true though, most HTTP servers handle multipart responses built from clients.
The good news is that "multipart" is just content type like XML or JSON, so you should be able to attach any old multipart parser to the response body after you've made the request with your favorite HTTP client.
Some parsers to consider:
https://github.com/danabr/multipart-parser
Rack::Multipart::Parser
Shoehorn your data into Rack::Utils.parse_multipart
As far as I understand, for AJAX generally js/jquery are used, so if I plan some AJAX actions for action, I should add format.js {...}.
I googled this problem and as far as i understand, main JSON use is for creating application's API.
Any other reasons to include format.json and json rendering in the application?
I think this is a common misunderstanding.
Sending http request through Ajax has nothing to do with rendering HTTP response in a specific format. Writing in the controller action fromat.js{.....} or format.html{...} has nothing to do with AJAX. it is just you are telling your controller action to respond based on the request format. regardless to how the request send.
When you send AJAX request you can determine the expected data type or content-type by using the following $.ajax attribute dataType: "json".
For creating APIs as far as I know most of apis if not all of them are using either xml or json. the latter has become very popular. Most of the newly built apis are built to send data in a json format.
So when you built you API you need to design you action response to be in a json format. There are many ways to do such a thing.
My favourite way is to build a json view this can be done using jbuilder which is included by default as a gem when you create a new rails app OR use the my favourite json builder gem which is rabl.
I found the MVC API Action filter which should automatically check if the request demands a json response and if so automatically serialize in json the model i was sending to the view, right ?!
http://mvcapi.codeplex.com/
I found many examples but the thing is they all assume that the request will be sent by an Ajax call in which i can clearly specify it's a json request.
I want to call the action directly from my browser but i'm not without any specification it simply returns the view
How do i specify in the url that i'm requesting a json response?
I know of the library that you write of, but haven't used it. I shied away from using it when I saw that it never seems to have made it out of Beta on Codeplex and hasn't been updated in over a year.
That aside, in the methodology being used, the URL doesn't determine the data type coming back, the Http Accept Headers are what does it. This is a more RESTful approach to returning data.
You'll note on the link that you provided in the Request section that
Accept: application/json, text/javascript, */*
application/json is what tells the service to return json. You may find other examples on the web that say text/json instead, and they should work also, but application/json is the correct standard.
If you're using jQuery, you can use $.ajax and specify dataType: 'json' or just use the $.getJson method directly.
I am dealing with a third-party api here and I need to send HTTP Post request represented in XML. How should I go about doing this in Rails? Which library/method if any will allow me to do this?
Try net/http package, in particular post method. There're examples too.
As to xml part, you can send any data you want as long as it's string.
A good starting point would be Net::HTTP library: http://stdlib.rubyonrails.org/libdoc/net/http/rdoc/index.html