0
votes

I am new to Ruby on Rails, I would like to pass some data to my other application. I am currently using httpparty gem but I am having a hard time when I send big data such as text so I was wondering if there is any recommended gem that converts plain html text to the format that I can send through or easy way to do

What I am trying to do:

e.g:

def abc_action 
    contents = params[:content]
    response = HTTParty.post("http://abc.go.com?contents=#{contents}")

end

say content params include many paragraphs such as

"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum"

Thanks for your help.

1
What's the problem with the given code? - Nico Haase
The problem is if the content params includes '&' or apostrophe ...etc it doesn't send the contents properly so was wondering if there is any gem or some way that converts text to url format automatically - awesomie
And what happens instead? Please share some examples where you're facing problems, and the expected output - Nico Haase
maybe you've got some issues with URI? You can convert text like so: URI.encode("Hello world &"). After sending the payload you can decode this with URI.decode("Hello%20world%20&"). All special characters will be untouched - nuaky
HTTParty will handle this for you when used appropriately. try HTTParty.post("http://abc.go.com",body: {contents: params[:content]}) instead. HTTParty::post. If it must be appended to the URL which seesm odd for a POST request then access would be as follows: HTTParty.post("http://abc.go.com",query: {contents: params[:content]}) - engineersmnky

1 Answers

0
votes

From what i can tel you're getting the "contents" from a POST request on your controller, which i'm guessing came from a form you created in one of your views. If that's the case you could skip the controller altogether and just point your form's action to http://abc.go.com. You can read about how to do it here.

<%= form_tag("http://abc.go.com", method: "post") do %>
  <%= label_tag(:contents, "Contents: ") %>
  <%= text_area_tag(:contents) %>
  <%= submit_tag("Submit") %>
<% end %>

This way the user's browser sends the request and you don't need to have an HTTP client on your controller. It is important to know that you have to take care of the authenticity token by hand and set up abc to redirect back to your original app or wherever you want with http 302.

If abc is a rails app then the controller could look like this:

class SomeController < ApplicationController
protect_from_forgery except: :create

def create
  #use strong params not params[:contents]
  @something = SomeModel.new(contents: params[:contents])

  if @something.save  
    format.html {redirect_to "http://original_url/place_to_go_after_content_submit")
  end
end