Skip to content
This repository has been archived by the owner on Aug 29, 2024. It is now read-only.

Elevate::HTTP Examples

Matt Green edited this page Jul 12, 2013 · 1 revision

Elevate's HTTP client differs a bit from other RubyMotion HTTP clients. Here's a short cookbook of examples to get you going.

Response Handling

Elevate returns the response synchronously, so you don't need to pass a block. The returned Response object has accessors for all of the usual HTTP fields of interest: headers, url, and so forth. The body field deserves special mention: if the Content-Type of the request is application/json, it will be automatically the decoded. Otherwise, it returns the raw request bytes as an instance of NSData.

Simple GET request

response = Elevate::HTTP.get("https://www.google.com/")

GET request with query parameters

response = Elevate::HTTP.get("https://example.com/list", query: { q: "search_term" })

GET request with a custom header

response = Elevate::HTTP.get("https://example.com/list", headers: { "API-Key" => "1234567890" })

POST request with a form body

response = Elevate::HTTP.post("https://example.com/register", form: { name: "matt", password: "secret" })

POST request with a JSON-encoded body

response = Elevate::HTTP.post("https://example.com/register", json: { name: "matt", password: "secret"})

POST request with a JSON-encoded body and HTTP Basic Authentication

credentials = { username: "matt", password: "secret" }
response = Elevate::HTTP.post("https://example.com/", credentials: credentials, json: { last_access: nil })

Credentials are not encrypted in transit, use HTTPS!