This question is about 2 rails apps I’m building. I’m trying to get my head around Rails and restful web services. Most of the code is pseudo but If required I suppose i could attempt some examples.
There are also a few basic rails and web requests thrown in.
Imagine I have rails programs A and B. I want A to send a post request which will basically contain an object formatted in JSON (not required but planned) and B to recieve it and turn it into the object I want. So heres my plan or concept at least and I’d love some feedback on it.
So focusing on B for the moment.
A url like this
127.0.0.1:8080/receive
would link to something like
def recieve (assuming routes are setup correctly)
@object = (this is where im a bit lost. how do i receive it from URL)
@request = UserRequest.new(params[:request])
A lot of tutorials seem to focusing on sending post requests but never receiving or processing them. anyone point me in the right direction?
And for A
I know how to send a post request
@stringParams = 'www.stackoverflow.com'
@hostURL = 'http://127.0.0.1:20000/requests/add'
#This is a post request that needs to post json to my recieveing crawler
res = Net::HTTP.post_form(URI.parse(@hostURL),
{'url' => @stringParams})
#puts res.body
I know how to create a JSON object from a hash
@myHash = HASH.new
@myHash =
{
"url" => 'www.stackoverflow.com'
#Assume theres more than one variable
}
@myJsonHash = @myHash.to_json
So the question for A is how do I send that @myJsonHash in a post request. Or is that something thats really stupid to do and have I misunderstood the requirments?
You’ll receive data in the parameters. You should know which params contain what.
Beware of cross sites posts, you should implement some sort of authentication.