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

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.

Related

Swift PerfectServer: POST request and JSON body

first of all I'd like to thank the team for this amazing project, it is indeed exiting to be able to start writing server-side software in Swift.
I'm successfully running a POC using PerfectServer on an Ubuntu VM and working on the API to interact with the mobile client.
There is one aspect I didn't quite understand yet, and that is accessing the request body data from my PerfectServer Handler.
Here is the workflow I have in mind:
The client submits a POST request to PerfectServer including some
JSON encoded body data
Once that hits the "valuesForResponse:" of
my server side Handler, I retrieve the WebRequest representation of
my request successfully
The request object does expose a many
properties of the HTTP request, including headers and the url-like
formatted query parameters.
Unfortunately, I cannot see a way to retrieve the underlying request body data. I would expect that to be some kind of public properties exposing the raw data that my handle can retrieve and decode in order to process the request.
The only example provided in the Examples workspace that comes with the project and sends a POST request that includes a body is in the project Authenticator. Here the HTTP body part takes the form os a UTF-8 encoded string where the values are query-params-like formatted.
name=Matteo&password=mypassword
This gets somehow exposed on the server handler by the WebRequest "param" property, that in the inner implementation of HTTPServer seems to expect an "&" separated string of key-values:
What I would expect is to have a way to provide body data in whatever form / encoding needed, in my case a JSON form:
{"name":"Matteo", "password":"psw"}
and be able to access that data from the WebRequest in my handler, decode it and use it to serve the request.
To summarise, I assume you could say that a WebRequest.bodyData public property is what I am after here :).
Is there something I am missing here?
Thanks in advance for any clarification!

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.

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

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.

How to keep Rails from Processing Large XML Post

In our rails application we have a many actions that do regular webapp actions. But, we have a single action that accepts a large XML file. I would like to keep rails from parsing the XML into params. Instead, I would like to be able to get the URL params ( /documents/{id}/action ) and then write out the xml file to a specific directory. How do I keep Rails from processing it?
How would I define the action to handle this?
def handle_xml
# what to put here
end
The upload is done using Content-Type: application/xml It is a single file, and not part of a multipart form. The sample curl statement would be:
curl-H 'Accept: application/xml' -H 'Content-Type: application/xml' -X POST -d '<?xml version="1.0" encoding="UTF-8"?><test></test>' http://0.0.0.0:3000/controller/handle_xml
If you want to prevent rails from automatically parsing the XML data into a hash of parameters, you'll have to replace the ParamsParser middleware with your own custom version.
When a file is posted to rails, the ParamsParser middleware modifies the request parameters and turns it into a Hash if the data format is xml. You can find the details in the params_parser.rb file in rails.
Here's a RoR mailing list message similar to the question that you've asked
Unfortunately, as a new user I can't post any more links, but you should search google with "Sanitizing POST params with custom Rack middleware" for some more details on writing custom rack middleware.
I too have come across this problem recently. However mine is in an internal application where I have full control over both the Rails app and the clients connecting to it.
In my app the client POSTs a large XML data set to the Rails app. I wanted to process the XML document in a delayed job (resque). My workaround was to make the client use an alternate content-type. I used application/octet-stream. This prevents Rails from parsing the POST data. The data is available in request.raw_post.
The action should receive it as a file (through way of multipart form upload) and then store it as a temporary file for you.
Have you tried sending the xml file has one variable in the http uri request? So something like
#xml_file = xml..xml...xml...
parameters => {
query => {
xml_file => #xml_file
}
}
Httparty.post("url", parameters)
Then in your method:
def handle_xml
#xml_file = params[:xml_file]
#xml_file.save (or whatever you want here..)
end

Resources