Lightweight REST library for iOS and watchOS. Available on cococapods

Overview

RMHttp

Build Status Status license Platform Version Twitter

RMHttp Lightweight RESTful library for iOS and watchOS RMHttp is a Lightweight REST library for iOS and watchOS.

Features

  • Chainable Request
  • URL / JSON Parameter Encoding
  • HTTP Methods GET/POST/DELETE/PATCH/PUT based in RFC2616
  • Custom Request Builder / HEADERS / PARAMETERS
  • Form-Data Support
  • Dynamic Response Handler (JSONObject, JSONArray, String)
  • Codable Support
  • Support Parameters Container RMParams
TODO:
  • [-] Support Upload/Download resource

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Installation

RMHttp is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'RMHttp'

Installation

pod "RMHttp"

Run the following command in your Terminal

$ pod install

Usage

HTTP Methods

HTTP Methods are declared in public enum RMHttpMethod.

GET, POST , DELETE , PUT , PATCH

Parameter Encoding

Encoding are declared in public enum Encoding

URLEncoding JSONEncoding

Serialization

JSONObject - a representation of Dictionary<String, Any>

{
   "data" : "value",
   "isBoolean" : true,
   "list": [
      "object1",
      "object2"
   ]
}

JSONArray - a representation of [Dictionary<String, Any>]

[
   { "data1" : "value1"},
   { "data2" : "value2"},
   { "data3" : "value3"},
]

String Any String respresentation (e.g HTML String, XML String, Plain Text)

Building Request

Building request with parameters from Dictionary type
let params = [
   "string":"Ipsum",   // String
   "number": 100,      // Number
   "boolean":true      // Boolean
] as [String : Any]

let urlString = "https://httpbin.org/get"
let request = RMRequest(urlString, method: .GET(.URLEncoding), parameters: params, hearders: nil)
Building request with parameters from RMParams container

URL query representation names[]=lorem&names[]=ipsum&names[]=dolor&

let lorem = RMParams(key: "names[]", value: "lorem")
let ipsum = RMParams(key: "names[]", value: "ipsum")
let dolor = RMParams(key: "names[]", value: "dolor")
let params = [lorem, ipsum, dolor]

let urlString = "https://httpbin.org/post"
request = RMRequest(urlString, .GET(.URLEncoding), params, nil)
return request

Chained Response Handlers

Expecting Array object Response
RMHttp.JSON(request: request) { (response:JSONArray?, error) in
   guard error == nil else {
      self.title = "Response Error"
	  self.activity.stopAnimating()
	  self.textView.text = "\(err)"
	  return
   }
   self.activity.stopAnimating()
   if let data = response {
      self.title = "Response Sucess"
      self.textView.text = "\(data)"
   }
}
Expecting JSON object Response
RMHttp.JSON(request: request) { (response:JSONObject?, error) in
   guard error == nil else {
      self.title = "Response Error"
	  self.activity.stopAnimating()
	  self.textView.text = "\(err)"
	  return
   }
   self.activity.stopAnimating()
   if let data = response {
      self.title = "Response Sucess"
	  self.textView.text = "\(data)"
   }
}

Generic method that return HTTP response has parameter data that comply to RMHttpProtocol (e.g JSONObject, JSONArray, String, )

public class func JSON<T:RMHttpProtocol>(request: RMRequest, completionHandler: @escaping Handler<T>)

FORM-DATA

Add fields from dictionary type
let params =  [
   "name":"lorem", 
   "lastName":"ipsum"
]
let urlString = "https://httpbin.org/post"
let request = RMRequest(urlString, .POST(.FomDataEncoding), params, nil)
return request
Add file

See Media Types

let request = RMRequest(url: URL(string: urlString)!)
request.addForm(field: "file", file: imgData, fileName: "image.jpeg", mimeType: "image/jpeg")
request.setHttp(method: .POST(.FomDataEncoding))
return request
Or manually add field
let request = RMRequest(url: URL(string: urlString)!)
request.addForm(fieldName: "field1", value: "lorem ipsum")
request.addForm(fieldName: "field2", value: "sit dolor amet")

Author

rogermolas, [email protected]

License

The MIT License (MIT)

Copyright (c) 2018-2020 Roger Molas

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

You might also like...
🌏 A zero-dependency networking solution for building modern and secure iOS, watchOS, macOS and tvOS applications.
🌏 A zero-dependency networking solution for building modern and secure iOS, watchOS, macOS and tvOS applications.

A zero-dependency networking solution for building modern and secure iOS, watchOS, macOS and tvOS applications. 🚀 TermiNetwork was tested in a produc

SwiftSoup: Pure Swift HTML Parser, with best of DOM, CSS, and jquery (Supports Linux, iOS, Mac, tvOS, watchOS)
SwiftSoup: Pure Swift HTML Parser, with best of DOM, CSS, and jquery (Supports Linux, iOS, Mac, tvOS, watchOS)

SwiftSoup is a pure Swift library, cross-platform (macOS, iOS, tvOS, watchOS and Linux!), for working with real-world HTML. It provides a very conveni

A delightful networking framework for iOS, macOS, watchOS, and tvOS.
A delightful networking framework for iOS, macOS, watchOS, and tvOS.

AFNetworking is a delightful networking library for iOS, macOS, watchOS, and tvOS. It's built on top of the Foundation URL Loading System, extending t

ZeroMQ Swift Bindings for iOS, macOS, tvOS and watchOS

SwiftyZeroMQ - ZeroMQ Swift Bindings for iOS, macOS, tvOS and watchOS This library provides easy-to-use iOS, macOS, tvOS and watchOS Swift bindings fo

A peer to peer framework for OS X, iOS and watchOS 2 that presents a similar interface to the MultipeerConnectivity framework

This repository is a peer to peer framework for OS X, iOS and watchOS 2 that presents a similar interface to the MultipeerConnectivity framework (which is iOS only) that lets you connect any 2 devices from any platform. This framework works with peer to peer networks like bluetooth and ad hoc wifi networks when available it also falls back onto using a wifi router when necessary. It is built on top of CFNetwork and NSNetService. It uses the newest Swift 2's features like error handling and protocol extensions.

Lightweight library for web server applications in Swift on macOS and Linux powered by coroutines.
Lightweight library for web server applications in Swift on macOS and Linux powered by coroutines.

Why Zewo? • Support • Community • Contributing Zewo Zewo is a lightweight library for web applications in Swift. What sets Zewo apart? Zewo is not a w

A lightweight but powerful network library with simplified and expressive syntax based on AFNetworking.
A lightweight but powerful network library with simplified and expressive syntax based on AFNetworking.

XMNetworking English Document XMNetworking 是一个轻量的、简单易用但功能强大的网络库,基于 AFNetworking 3.0+ 封装。 其中,XM 前缀是我们团队 Xcode-Men 的缩写。 简介 如上图所示,XMNetworking 采用中心化的设计思想

A lightweight, one line setup, iOS / OSX network debugging library! 🦊
A lightweight, one line setup, iOS / OSX network debugging library! 🦊

Netfox provides a quick look on all executed network requests performed by your iOS or OSX app. It grabs all requests - of course yours, requests from

Super lightweight async HTTP server library in pure Swift runs in iOS / MacOS / Linux

Embassy Super lightweight async HTTP server in pure Swift. Please read: Embedded web server for iOS UI testing. See also: Our lightweight web framewor

Comments
  • FORM DATA  - support file type encoding

    FORM DATA - support file type encoding

    let request = RMRequest(url: URL(string: urlString)!)
    request.setHttp(hearders: APIRequestBuilder.getHeaders())
    request.setHttp(method: .POST(.FomDataEncoding))
    
    let boundary = UUID().uuidString
    request.setHttp(hearders: ["Content-Type":"multipart/form-data; boundary=\(boundary)"])
    
    var data = Data()
    for (key, value) in params! {
       if key == "file" {
          // Add the image data to the raw http request data
          let fileName = "profile.jpg"
          data.append("\r\n--\(boundary)\r\n".data(using: .utf8)!)
          data.append("Content-Disposition: form-data; name=\"\(key)\"; filename=\"\(fileName)\"\r\n".data(using: .utf8)!)
          data.append("Content-Type: image/jpeg\r\n\r\n".data(using: .utf8)!)
          data.append((value as! UIImage).jpegData(compressionQuality: 1)!)
          data.append("\r\n--\(boundary)--\r\n".data(using: .utf8)!)
       } else {
          data.append("\r\n--\(boundary)\r\n".data(using: .utf8)!)
          data.append("Content-Disposition: form-data; name=\"\(key)\"\r\n\r\n".data(using: .utf8)!)
          data.append("\(value)".data(using: .utf8)!)
      }
    }
    // set as httpbody
    request.setHttpBody(data: data)
    
    enhancement 
    opened by rogermolas 0
Releases(v1.6.1)
  • v1.6.1(Feb 10, 2020)

    var files = [RMFormDataFile]()
    let file = RMFormDataFile("picture", image.jpegData(compressionQuality: 1.0), "avatar.jpeg", "image/jpeg")
    files.append(file)
    
    let params = [
       "field1":  arbitration.auction.id!,
       "description":  "sample test"
    ]
    
    let request = APIRequestBuilder().buildFormRequest(api: urlString, params: param, files: files)
    
    Source code(tar.gz)
    Source code(zip)
  • v1.6.0(Feb 10, 2020)

    let name1 = RMParams(key: "names[]", value: "roger")
    let name2 = RMParams(key: "names[]", value: "molas")
    let booleanType = RMParams(key: "boolean", value: true)
    let number = RMParams(key: "number", value: 100)
    let floatNumber = RMParams(key: "float", value: 1.0)
    let params = [name1, name2, booleanType, number, floatNumber]
    
    let urlString = "https://httpbin.org/post"
    request = RMRequest(urlString, .GET(.URLEncoding), params, nil)
    return request
    

    Form-data Add file

    let request = RMRequest(url: URL(string: urlString)!)
    request.addForm(field: "file", file: imgData, fileName: "image.jpeg", mimeType: "image/jpeg")
    request.setHttp(method: .POST(.FomDataEncoding))
    return request
    

    Updates Added:
    #6 #5

    Source code(tar.gz)
    Source code(zip)
  • v1.5.0(Jan 13, 2020)

    Add form-data fields from params

    RMRequest(urlString, method: .POST(.FomDataEncoding), parameters: params, hearders: nil)
    

    Custom form data request

     let request = RMRequest(url: URL(string: urlString)!)
     request.setHttp(method: .POST(.FomDataEncoding))
     request.setFormData(fields: params)
    

    or

     request.setHttpBody(data: customDataBody)
    
    Source code(tar.gz)
    Source code(zip)
  • v1.4(May 7, 2019)

  • v1.3.1(Sep 23, 2018)

Owner
Roger Molas
Mainly work with Objective-C, Swift, C/C++, Python and React Native
Roger Molas
QwikHttp is a robust, yet lightweight and simple to use HTTP networking library for iOS, tvOS and watchOS

QwikHttp is a robust, yet lightweight and simple to use HTTP networking library. It allows you to customize every aspect of your http requests within a single line of code, using a Builder style syntax to keep your code super clean.

Logan Sease 2 Mar 20, 2022
Lightweight Networking and Parsing framework made for iOS, Mac, WatchOS and tvOS.

NetworkKit A lightweight iOS, Mac and Watch OS framework that makes networking and parsing super simple. Uses the open-sourced JSONHelper with functio

Alex Telek 30 Nov 19, 2022
Swift/Obj-C HTTP framework with a focus on REST and JSON

Now Archived and Forked PMHTTP will not be maintained in this repository going forward. Please use, create issues on, and make PRs to the fork of PHMT

Postmates Inc. 509 Sep 4, 2022
Simple REST Client based on RxSwift and Alamofire.

RxRestClient Example To run the example project, clone the repo, and run pod install from the Example directory first. Requirements iOS 10.0+ tvOS 10.

STDev 16 Nov 19, 2022
Simple REST Client based on RxSwift and Alamofire.

RxRestClient Example To run the example project, clone the repo, and run pod install from the Example directory first. Requirements iOS 10.0+ tvOS 10.

STDev 16 Nov 19, 2022
The civilized way to write REST API clients for iOS / macOS

The elegant way to write iOS / macOS REST clients Drastically simplifies app code by providing a client-side cache of observable models for RESTful re

Bust Out 2.2k Nov 20, 2022
TheraForge's Client REST API framework to connect to TheraForge's secure CloudBox Backend-as-a-Service (BaaS)

OTFCloudClientAPI TheraForge's Client REST API Framework to Connect to TheraForg

TheraForge 0 Dec 23, 2021
This is my attempt to make this repo available on swift package manager

Parse LiveQuery Client for iOS/OSX PFQuery is one of the key concepts for Parse. It allows you to retrieve PFObjects by specifying some conditions, ma

Benji 0 Nov 3, 2021
Commonly available global error type packages

ErrorKit 일반적으로 사용 할 수 있는 전역 오류 타입 패키지. Requirements Xcode 13.2.1+ Swift 5.5+ Ins

Jaemyeong Jin 0 Nov 6, 2022
A networking library for iOS, macOS, watchOS and tvOS

Thunder Request Thunder Request is a Framework used to simplify making http and https web requests. Installation Setting up your app to use ThunderBas

3 SIDED CUBE 16 Nov 19, 2022