Giter Site home page Giter Site logo

biatoms / request.swift Goto Github PK

View Code? Open in Web Editor NEW
15.0 3.0 3.0 50 KB

A tiny HTTP client written in swift. URLSession alternative

License: MIT License

Swift 96.09% Ruby 2.08% Objective-C 1.82%
http-client httpclient async asynchronous sync synchronous swift linux macos tvos

request.swift's Introduction

Platform Linux Platform Cocoapods Compatible Carthage Compatible License Build Status - Master

Request.swift

A tiny (sync/async) HTTP client written in swift.

OS

Works in linux, iOS, macOS and tvOS

Example

client.request("http://example.com", headers: ["Accept": "text/html"]).response { response, error in    
        if let response = response {
            print(response.statusCode)
            print(String(cString: response.body))
        } else {
            print(error)
        }
    }

Installation

CocoaPods

CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:

$ gem install cocoapods

To integrate Request.swift into your Xcode project using CocoaPods, specify it in your Podfile:

source 'https://github.com/CocoaPods/Specs.git'
target '<Your Target Name>' do
pod 'Request.swift', '~> 2.3.0'
end

Then, run the following command:

$ pod install

Carthage

Carthage is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks.

You can install Carthage with Homebrew using the following command:

$ brew update
$ brew install carthage

To integrate Request.swift into your Xcode project using Carthage, specify it in your Cartfile:

github "BiAtoms/Request.swift" ~> 2.3.0

Run carthage update to build the framework and drag the built RequestSwift.framework into your Xcode project.

Swift Package Manager

The Swift Package Manager is a tool for automating the distribution of Swift code and is integrated into the swift compiler. It is in early development, but Request.swift does support its use on supported platforms.

Once you have your Swift package set up, adding Request.swift as a dependency is as easy as adding it to the dependencies value of your Package.swift.

dependencies: [
    .package(url: "https://github.com/BiAtoms/Request.swift.git", from: "2.3.0")
]

Authors

See also the list of contributors who participated in this project.

License

This project is licensed under the MIT License - see the LICENSE file for details

request.swift's People

Contributors

orkhanalikhanov avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

request.swift's Issues

Status code 301 using GithubAPI

First, thanks for this library, it's a lot simpler than URLSession.

I was testing this lib using the Github API but I always get 301 moved permanently, even if I call the URL on the Location header.
Here is my code

class ViewController: UIViewController {
    
    private let client = Client(baseUrl: "http://api.github.com", usesSystemProxy: true)
    private let headers = ["Accept": "application/vnd.github.v3+json"]

    override func viewDidLoad() {
        super.viewDidLoad()
        
        request(url: "orgs/square/repos")
    }
    
    private func request(url: String) {
        let requester = client.request(url, method: .get, parameters: nil, headers: headers)
        requester.response { (response, error) in
            if let error = error {
                self.handleError(error)
            } else if let response = response {
                self.handleResponse(response)
            }
        }
    }
    
    private func handleError(_ error: Error) {
        print(error)
    }
    
    private func handleResponse(_ response: Response) {
        print(response)
        
        if response.statusCode == 301 {
            let newURL = response.headers["Location"]!
            request(url: newURL)
        } else {
            print(response.reasonPhrase)
            print(response.statusCode)
            
            let data = Data(bytes: response.body)
            let string = String(cString: response.body)
            print(string)
            let jsonArray = try! JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.init(rawValue: 0)) as! [[String: Any]]
            
            print(jsonArray.debugDescription)
        }
    }
}

I tried with others endpoints and got the same result.
Am I doing something wrong?
Can you help me?

Can't parse body if response header doesn't have 'Content-Length'.

I want to get some image data from: Here.
But when I try the request method, I find that the body property is alway empty. After inspecting the source code, I find where the issue is:

    open func parse() throws -> Response {
        let (version, code, reason) = try parseStatusLine()
        let headers = parseHeaders()
        var body = [Byte]()
        if let value = headers[.contentLength], let length = Int(value) {
            body = try parseBody(length)
        }
        
        return Response(code: code, reasonPhrase: reason, version: version, headers: headers, body: body)
    }

The response header doesn't have a Content-Length, because it uses Transfer-Encoding.

And another problem I find is that when I send a same HTTPS request(.get) to Here repeatedly, it randomly throws different errors. But sometimes it works well. I can't remember the error descriptions clearly, but it looks like this: Error 0 unknown issue / Error 2 No such file, Error? exceed maximum length)

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.