Pavel Tkackenko Pavel Tkackenko - 3 months ago 18
HTTP Question

(Rails app Api) Send data via HTTP and receive JSON or XML

I'm building a simple calculator for courier company. This calculator should receive some data via http request: weight, origin city and destination city. Then calculator should send back JSON or XML: price.

Sounds very simple, but I can't find any usable information how to handle with http request.

As I understand the request should be something like this:

localhost?weight=20&origin="Almaty"&destination="Moscow"


But I have no idea how to handle it with Rails. How to obtain these variables in HTTP?

Answer

You have to use sockets so the client get make a http request, you receive it on your socket, compute it and send him back the result.

Documentation for url attributes parser: http://www.ruby-doc.org/stdlib-2.0/libdoc/cgi/rdoc/CGI.html#M000075

this might help edit:

require 'cgi'

CGI::parse('param1=value1&param2=value2&param3=value3')


returns you sth like that:

{"param1"=>["value1"], "param2"=>["value2"]}
Comments