Giter Club home page Giter Club logo

kitura-request's Introduction

KituraRequest

Warning: This is work in progress

A module for sending HTTP requests in IBM Kitura based applications. It wraps KituraNet ClientRequest class and exposes a familiar interface known from Alamofire.

TODO:

  • !! Add URL parameter encoding
  • !! Add JSON parameter encoding
  • !! Multipart form data parameter encoding
  • !! Make tests run on Linux
  • !! Write tests to check if resulting ClientRequest is properly initialised
  • Add synchronus interface
  • Add async interface
  • Write instructions
  • Switch back to depend on IBM Kitura-net

Installation

To install KituraRequest add following line to Dependencies in Package.swift:

.Package(url: "https://github.com/IBM-Swift/Kitura-Request.git", majorVersion: 0)

Currently Swift 3.0 Release is supported

Usage

API of KituraRequest should feel familiar as it closely maps the one of Alamofire.

Creating a request

To create a request object simply call

KituraRequest.request(.get, "https://httpbin.org/get")

Request parameters and parameters encoding

You can also create a request with parameters by passing [String: Any] dictionary together with an encoding method:

KituraRequest.request(.post,
                      "https://httpbin.org/post",
                      parameters: ["foo":"bar"],
                      encoding: JSONEncoding.default)
KituraRequest.request(.post,
                      "https://httpbin.org/post",
                      parameters: ["foo":"bar"],
                      encoding: URLEncoding.default)
KituraRequest.request(.post,
                      "https://httpbin.org/post",
                      parameters: ["foo":"bar"],
                      encoding: MultipartEncoding([
                        BodyPart(key: "file", data: data, mimeType: .image(.png), fileName: "image.jpg")
                    ]))

Currently URLEncoding, JSONEncoding and MultipartEncoding encodings is supported by default.

URLEncoding encodes parameters as URLs query.
JSONEncoding converts parameters dictionary to JSON and appends it to request's body.
MultipartEncoding generates multipart http body from passed parameters and appends it to request's body.
When encoding parameters as JSONEncoding or MultipartEncoding appropriate Content-Type header is set.
To create custom parameter encoding extend any class or struct with Encoding protocol.

Headers

To set headers in the request pass them as [String: String] dictionary as shown below:

KituraRequest.request(.get,
                      "https://httpbin.org/get",
                      headers: ["User-Agent":"Awesome-App"])

Handling response

Currently there is only one method that you can call to get back the requests response and it returns NSData.

KituraRequest.request(.get, "https://google.com").response {
  request, response, data, error in
  // do something with data
}

License

This library is licensed under Apache 2.0. For details see license.txt

kitura-request's People

Contributors

akrabat avatar bdhernand avatar ianpartridge avatar ivanandriollo avatar michalkalinowski- avatar mman avatar na-gupta avatar naithar avatar rob-deans avatar shmuelk avatar vadimeisenbergibm avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.