Cross-domain ajax post with JSON data going to Rails controller as HTML - ruby-on-rails

I'm making a cross-domain POST request and have set up headers in Rails 3.2 to accept the request appropriately - both the OPTIONS and POST request are received by the controller correctly.
The problem I'm finding is that Rails controller is receiving the JSON data as HTML. Why is this? How can I force the controller to treat it as JSON? (doing this from the client-side would be preferable)
If this is unclear let me know which messages/code I ought to post for better assistance. Thanks

You could try adding the JSON extension to the URL that you're making a request to, i.e. https://your_domain.com/your_url.json, which should force the JSON format.

Related

How to get Rails to respond with json based on headers

I'm building an API with Rails 4 and in my controller I'm using respond_to to differentiate between html and json request which all works fine. While testing my API in Postman, I've added the following header: Content-Type: application/json but in my request I still need to add .json at the end of it like so: https://myapi.com/api/users.json otherwise Rails will respond with html even though the aforementioned header is present.
My question is this: is there a way that Rails can recognise the Content-Type: application/json header and respond with json without me using the .json part in the url like so: https://mypai.com/api/users?
You also need to set the Accept header to Accept: application/json
If you're curious as to why, and the difference between the two headers, the answers to this post explain it best: https://webmasters.stackexchange.com/questions/31212/difference-between-the-accept-and-content-type-http-headers
If you are using any link helper inside a view, simple add the format information, like posts_path(format: :json).

How can I log the raw http request/response in rails?

Is there any way to log the raw http requests/responses in rails?
that is, the plain text http message, including the body, even if the body is binary
It depends a bit on what exactly you want to log. But I believe raw_post is what you are looking for. This will return the raw POST body. You can call this method in any of your methods in the controller.
A similar method exist for getting the GET params: raw_params
If that isn't good enough then a middleware layer might help.

get GET request in Golang on fcgi

I run my scripts under Apache. I understand how I can create request, for example:
http.Get(url)
How I can get GET request? I really dont see this information in docs. Thanks in advance!
UPD
For example, i do GET or POST-request to my go script from another script. In PHP I'd just write $a=$_GET["param"]. How i can do that in Go? Sorry for bad english, by the way
Your handler is passed a Request. In that Request you find for example the parameters in the Form field as soon as you parsed it using ParseForm :
// Form contains the parsed form data, including both the URL
// field's query parameters and the POST or PUT form data.
// This field is only available after ParseForm is called.
// The HTTP client ignores Form and uses Body instead.
Form url.Values

POST request in rails

I need to create a post request in a somewhat weird(speaking leniently) format. The exact request to be sent should be in the following format
https://xyz.com/ping?app_id=
123&adv_id=345&event=sale&event_data="amt=30_USD;user_id=204050"
Its easy to send a post request to an url of the following format :-
https://xyz.com/ping?app_id=
123&adv_id=345&event=sale&amt=30_USD&user_id=204050
This can be achieved using code like this :-
Net::HTTP.post_form(URI.parse("http://xyz.com/ping"), params)
where, the params variable is appropriately populated(hash).
What modification should i make to account for this change from normal scenario, particularly to account for the double quotes around event data.
Read and adapt the information from the links below. After going through them, I can deduce 4 possible ways to do this:
Use Mechanize. See link 1
Do a post request from your controller using Net::HTTP. See link 2 - 3(3rd answer).
Post form data containing a hash or array. See links 4 - 7
Add hidden field to your form that will contain the extra data. See link 8
Use the params merge pattern ie Link 9
Using Ruby/Rails To Programmatically Post Form Data To Another Site
Submitting POST data from the controller in rails to another website
Post and redirect to external site from Rails controller?
http://rails.nuvvo.com/lesson/6371-action-controller-parameters
http://www.developer.com/lang/rubyrails/article.php/3804081/Techniques-to-Pass-and-Read-URL-Parameters-Using-Rails.htm
http://guides.rubyonrails.org/action_controller_overview.htm (the section 3.1 Hash and Array Parameters, then section 8 on Request Forgery Protection)
Rails: How do I make a form submit an array of records?
Ruby on Rails: Submitting an array in a form
http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-hidden_field
Send querystring params as part of form post
You need to make sure that the event_data parameter is properly escaped to do this. I'm pretty sure that calling post_form will do this for you already.
params["app_id"] = 123
params["adv_id"] = 345
params["event"] = 'sale'
params["event_data"] = '"amt=30_USD;user_id=204050"'
Net::HTTP.post_form(URI.parse("http://notify.tapsense.com/ads/ping"), params)
That should more or less do it for you.

How to POST JSON in body for functional tests in Symfony 1.4

I'm writing some functional tests for a POST API endpoint. I've reviewed the documentation and can't find a way to add content to the POST body. The post method for sfBrowser:
post('some url',array('x'=>'y'))
Only creates POST parameters (in this case x=y). Is there anyway of adding content to the post body using sfBrowser?
From what I have found here, here and here, the POST format takes parameter:value format, so you can send your JSON with some code like:
post('some url', array('json_data' => json_encode($toJson))
and then decode in your action with
$jsonObj = json_decode($request->getParameter('json_data'));
but you need to associate your JSON data with a parameter name in your POST to retrieve it on the server side.
As a side note, after looking at the Symfony code, the parameters are given straight to $_POST except for CSRF, which is tweaked.

Resources