Pubsubhubbub on Rails. How to extract the raw POST body contents from the POST request? - ruby-on-rails

I am having trouble setting up a pubsub enabled subscriber app using rails. I have currently subscribed to the open hub pubsubhubbub.appspot.com and am receiving pings to my application's endpoint. (as of now i have created a counter which increments everytime the end point is pinged). But i am not able to understand as to how to extract the raw POST body contents from the POST. I am new to pubsub and am eager to experiment with it. I came across this blog post but it is not language specific.
Source: Joseph Smarr: Implementing PubSubHubbub subscriber support: A step-by-step guide. http://josephsmarr.com/2010/03/01/implementing-pubsubhubbub-subscriber-support-a-step-by-step-guide/
Now you’re ready for the
pay-out–magically receiving pings from
the ether every time the blog you’ve
subscribed to has new content! You’ll
receive inbound requests to your
specified callback URL without any
additional query parameters added
(i.e. you’ll know it’s a ping and not
a verification because there won’t be
any hub.mode parameter included).
Instead, the new entries of the
subscribed feed will be included
directly in the POST body of the
request, with a request Content-Type
of application/atom+xml for ATOM feeds
and application/rss+xml for RSS
feeds. Depending on your programming
language of choice, you’ll need to
figure out how to extract the raw POST
body contents. For instance, in PHP
you would fopen the special filename
php://input to read it.
Any help would be greatly appreciated.

You didn't say but I'm assuming you are running Rails 3.x?
To get the raw POST body you simply use request.raw_post in your controller. This will give you a long string that looks like a request parameters string: some_var=something&something_else=something_else... which you can then parse to get at what you want.
However, look at you development logs for an incoming request and see if the params hash isn't a better option for you. The service should post the data under some variable name, such as some_var above, and the params hash will hold an params[:some_var] containing only that data. No need for you to dig it out on your own in other words.

Related

Ping/Post Form Handling with PHP?

I'm working with a company on lead delivery, and they sent me some info regarding a Ping Post form setup. I've built hundreds of HTML forms processed by PHP (ie. sending an email/etc), but never something that would Ping a url, then return a value. The value it returns is XML.
Here's the purpose of the process:
I send a lead (form data) using the form with a particular zip code
This company parses that info, decides if it wants to "buy" it
Returns XML saying "Approved" or "Denied"
If "approved", I then post the data, and if "denied", I can do whatever I want
What is a common PHP method for doing this? I can research the code and put something together, just need to know what structure or PHP methods would work?
Thanks in advance.
You should be looking into RESTful Web Services.
here's a few examples that might help you
http://markroland.com/blog/restful-php-api/
http://coreymaynard.com/blog/creating-a-restful-api-with-php/
I did not create these examples, just what I found on Google.
I used file_get_contents(url) to handle the posting. The url contains inputs from the HTML form added as a query string, and the response is in XML which gets handled with simplexml_load_file().
As far as I understand your question what you need is to make an HTTP POST request and parse the incoming XML data.
I would rather not use file_get_contents() on remote servers - there are some potential security issues and it was missing some features the last time I checked. I strongly recommend cURL for remote HTTP/HTTPS communication.
Depending on the API you are posting to you might be able to use the SOAPclient class, but from the look of the response you got all you need is XML parser or Simple XML.
Anyway if you just need to check if a certain keyword (like Approved or Denied) is present you can use a simple string matching like this
if(strpos($response,'<STATUS>APPROVED</STATUS')!==false){
//approved
}
...

Ruby on Rails: Capturing JSON data from webhook?

I'm trying to understand how webhook works. My understanding is that its the ability to connect two different applications. If I submit a webhook with url
localhost:3000/receiver
to one application, and I have my application with a method
def receiver
end
I was wondering if I don't know what the callback is from the webhook would be, how would I capture data? How do I save any JSON data thats communicating with my application? I was thinking maybe save some file to see what the objects are, but I'm still fairly new and not sure how to capture JSON data?
Thanks
If you are sure that the webhook is returning a JSON, you can so something like this
data_json = JSON.parse request.body.read
Sure, a webhook a is tool to sincronize two apps
You HAVE tou know the structure of the incoming json, because you need to get the info inside
By definition a webhook is sent by POST method, so you can capture it just inspecting the body of the petition, i.e.
webHook = JSON.parse(params[:something])
Your would try with github web hooks and publish your app in heroku, the api is very well documented and there are many examples.

How do you create a web hook handler in Asp.net MVC 4?

My issue is that I do not know how to process an incoming request without knowing the URL that the post message is coming from.
The Vend API that I am trying to integrate with sends a post message to my URL with the following information:
"The POST body will include a field named payload. This field contains
a JSON encoded object with details of the object that caused the
hooked event. Other form fields (such as environment or domain_prefix)
may be present, but are not guaranteed to be.
The payload objects you’ll find in webhook requests are now the same
as those you’ll receive from the newer parts of the API marked as
version 1.0 or higher. So, for example, the product webhook should
give you a product payload that’s the same as if you requested
/api/1.0/product/{product_id}."
http://docs.vendhq.com/webhooks.html (here is the link for more details)
I'm fairly new to ASP MVC and I'm having trouble figuring out the best way to go forth. I need to eventually map the incoming name value pairs to my model.
Any help would be greatly appreciated.
When you said you don't know the URL that the post message is coming from, I think you mean that you don't know the format of the URL (path and parameters) that the webhook is being posted to. The only things your MVC app needs to know in order to process the request are the names of the post parameters, which in this case is a single parameter named payload that contains a JSON string according to the snippet you included from the documentation.
There are several ways for you to get this data. One of the most simple is to take advantage of ASP.NET MVC's automatic mapping of POST parameters to controller action method parameters:
public ActionResult MyActionMethod(string payload)
{
// 'payload' will be automatically populated with the json string from the POST payload
}
Of course you now have to parse the json payload which you can do in several ways using the .NET framework or with any number of 3rd party libraries.

EBAY - Sending a file via http post?

I've a file I want to send to the ebay system to support the LMS.
All the samples I've found include the use of the API, but the environment I'm working in doesn't have the ability to use it (the api).
So I'm forced to send the file with an HTTP post. But the doc's seem lacking.
Has anyone constructed/found an example of a HTTP post that will send a given file.
EDIT:
Oh.. what I see in the samples I have found is an area that seems it's supposed to have the data, but in the sample, there's nothing I'd consider real data.
Are you talking about the file transfer service or the bulk upload service? Don't you just generate an xml document and post the url like in this example:
http://developer.ebay.com/DevZone/file-transfer/CallRef/uploadFile.html#Samples

How to grab POST params (using Rails) in original order in which they came?

Hey there! I'm integrating Paypal's IPN system, and their authentication process requires my server to respond to a Paypal request with a POST to Paypal with parameters EXACTLY the same as Paypal sent me (although with an appended string). So, I've turned the params hash into a string (appropriately formatted) and sent it back, but it looks like the resulting string is not in the same order as the original POST parameters (resulting in no authentication).
Anyone know how I can access the actual POST parameters themselves (as opposed to the params hash), so that I can copy that and send it back to Paypal? The catch is that the order of parameters that I send to Paypal needs to be exactly the same as the request that hit my server.
Does that make sense? Any thoughts? Thanks in advance!
--Jared
Paypal's IPN can be a little finicky if I remember correctly (You really have to give them exactly what they sent with your message appended or nothing works).
You should probably grab the raw post data in your controller, append a string to it, and construct some sort of HTTP request with your raw post data before posting it back. I remember doing it the last time I used paypal's IPN, and it worked relatively well.
I would use ActionController#raw_post and just modify it before sending a request back to paypal. Also keep in mind there is a timeout for IPN responses (I think) and paypal may send you multiple notifications with the exact same data if they don't get back what they expect, so you probably want to send them a request back immediately before your code does anything else.
You cannot use the params hash, ordering is lost. Use the original request strings, for example request.request_uri or request.query_string.

Resources