Skip to content

Instantly share code, notes, and snippets.

@droberts-sea
Created November 9, 2016 22:54
Show Gist options
  • Save droberts-sea/e1ab4b9d128844da6821368add987778 to your computer and use it in GitHub Desktop.
Save droberts-sea/e1ab4b9d128844da6821368add987778 to your computer and use it in GitHub Desktop.
Sending data to an API via HTTParty
# For a POST-style request, sending the data in the request body
# Note the .to_json on the body data
url = 'http://localhost:1337/pets/'
HTTParty.post(url,
headers: {"Content-Type" => "application/json"},
body: { "pet"=> { "name" => "spike" } }.to_json
)
# For a GET-style request, sending the data in query params
# (that's the stuff after the '?' in the URL)
# Remember that we used a different URL for GET than for POST
# Note that the query argument does *not* have a .to_json
url = 'http://localhost:1337/pets/create'
HTTParty.get(url,
headers: {"Content-Type" => "application/json"},
query: { "pet"=> { "name" => "spike" } }
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment