Durchführen von JSON-API-Aufrufen innerhalb der Rails-App

Lesezeit: 1 Minute

Wir haben eine Instanz von WordPress bereitgestellt und eine installiert JSON-API-Plugin

Mit diesem Plug-in können Sie einfache HTTP-GET-Anforderungen wie die folgenden erstellen:

http://www.example.org/?json=get_recent_posts
http://www.example.org/?json=get_post&post_id=47
http://www.example.org/?json=get_tag_posts&tag_slug=banana

Und eine Antwort kommt in JSON zurück:

{ "status": "ok", "count": 1, "count_total": 1, "pages": 1, "posts": [
{...}}

My question is how should I go about making these types of requests (and then parsing the returned JSON) in my Rails app. I was about to get started just writing something from scratch but I imagine there’s some good existing tools out there to manage something like this. Basically I’ve never attempted connecting to an API when a Ruby-wrapper wasn’t already available. Can you help a noob with how to get started?

It looks like HTTParty is probably your best case for something like this. I think it’ll be an easy way for you to get started and handles the JSON for you easily.

Add ‘httparty‘ to your Gemfile and bundle.

recent_posts = HTTParty.get "http://www.example.org/?json=get_recent_posts"
puts recent_posts.status, recent_posts.count

recent_posts.posts.each do |post|
  puts post.title #guessing
end

1401410cookie-checkDurchführen von JSON-API-Aufrufen innerhalb der Rails-App

This website is using cookies to improve the user-friendliness. You agree by using the website further.

Privacy policy