Generate form post from local log to test a webhook? - ruby-on-rails

I'm trying to resolve a complicated form post in json format from the shopify CreateOrder webhook
To simplify testing and coding I wanted to recreate the post from the parameters = in the server. Output so that I don't have to create a cart each test cycle
This is an example from my log file:
Started POST "/shopify?id=123456&shopifyaction=OC&incomingpipe=6" for xxx.xxx.xxx.xxx at 2021-03-09 14:24:22 +0000
Processing by ShopifyWebhooksController#shopify_webhook as TEXT
Parameters: {"A1984"=>"1", "A9873"=>"5", "A1674"=>"2", "A8724"=>"1", "A3574"=>"3", "A1165"=>"5", "wbid"=>"162", "shopifyaction"=>"OC", "incomingpipe"=>"6"}
[WB] Shopify Post Verified
I wanted to grab the parameters hash in this and post it from an ajax or controller but I'm going round and round in circles as I try to convert this to coffee script / or a json or anything to parse it into a form, manly because the actually real post from shopify is a horrendous confusion of madness. and even with a good text editor find replace I'm throwing errors everywhere just to get jQuery to read it
Is there a simple obvious process for doing this in the controller that I'm missing?

For anybody looking for a good solution to this,
Yaro's fab Gem REPOST solves all the hassles
https://github.com/vergilet/repost

Related

Using Rails globbing for URL parameters: some URL types get cut off

I'm having an odd problem with Ruby on Rails globbing. My route looks like this:
get 'people/info_from_url/*url', to: 'people#info_from_url'
So from the frontend, I have get requests to URLs like:
my-api.com/people/info_from_url/youtube.com/XXX
my-api.com/people/info_from_url/twitter.com/XXX
my-api.com/people/info_from_url/tinyurl.com/XXX
...
These all work as expected - I get a parameter in the people#info_from_url controller action called url that contains the full URL that was sent.
However, for one particular kind of URL (XXX.carrd.co), the last part gets cut off. In the frontend, I send a get request to my-api.com/people/info_from_url/XXX.carrd.co. From the backend logs:
INFO -- : Started GET "/people/info_from_url/XXX.carrd.co/"
INFO -- : Processing by PeopleController#info_from_url as
INFO -- : Parameters: {"url"=>"XXX.carrd"}
Somewhere, the .co gets dropped. I'm not sure why this could be happening or how to debug it, since the change happens before I'm able to access the params hash. I could deal with this manually by just checking if it's a carrd link, but I'd like to know why it's happening and if any other kind of link might experience this issue. Thanks for any help!
Routes have an implicit optional (.:format) segment at the end. You can use this option to prevent that:
get 'people/info_from_url/*url', to: 'people#info_from_url', format: false
EDIT: you can check the last part here in the docs explaining that too https://guides.rubyonrails.org/routing.html#route-globbing-and-wildcard-segments

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.

Using restclient with multipart posts

I'm using restclient for a multipart form to send data to a restful web service (it's Panda video encoding service).
The trick though, is that the file I am passing into restclient (Technoweenie branch) is coming from my own form that a user submits.
So, lets walk through this. A user posts a file to my rails app. In my controller, it receives the file from params[:file]. I then want to pass params[:file] down to Panda using RestClient.
The error I'm getting is on the Panda server follows. I noticed that the file param in the stack trace is in a string as well (which I assume is Panda turning into a string for a nicer stacktrace).
~ Started request handling: Wed Aug 12 18:05:15 +0000 2009
~ Params: {"format"=>"html", "multipart"=>"true", "account_key"=>"SECURE_KEY", "action"=>"upload", "id"=>"SECURE_ID", "controller"=>"videos", "file"=>"#<File:0xcf02ca4>"}
~ 9bfb1750-6998-012c-4509-12313900b0f6: (500 returned to client) InternalServerErrorcan't convert nil into String
/var/local/www/panda/app/models/video.rb:246:in `extname'
/var/local/www/panda/app/models/video.rb:246:in `initial_processing'
/var/local/www/panda/app/controllers/videos.rb:79:in `upload'
I doubt you can really pass a CGI-style upload param from Rails into restclient and expect it to work.
A regular upload in Rails would have quite some extra attributes which do not belong in a posted resource (like the original filename and so on), and a Rails upload contains an IO with the actual file data. Also a file upload object in Rails might be a Tempfile handle and might be a StringIO - depending on the size of the upload.
What you effectively need to do is "repackage" your upload for rest-client to handle it properly, and pass the repackaged and rewound Tempfile object to restclient. Maybe you can get away with just picking the upload object itself instead of the whole params[:file]
Confirm that your restclient action can save locally first. If the action cannot save locally, then you will have a better idea where to look while trouble shooting.
Looks like the problem is with rest-client's posting of the file, check out an alternative method for posting like curb.
Lots of examples for posting multipart form data on this question: Ruby: How to post a file via HTTP as multipart/form-data?

Resources