Created
November 9, 2016 22:54
-
-
Save droberts-sea/e1ab4b9d128844da6821368add987778 to your computer and use it in GitHub Desktop.
Sending data to an API via HTTParty
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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