box*_*oxi 8 linux httprequest swift
我们想要发出HTTP(S)请求(GET)来调用API.问题是,NSURLRequest(目前)尚未在Linux for Foundation中实现(https://github.com/apple/swift-corelibs-foundation/blob/master/Foundation/NSURLRequest.swift).
是否还有其他创建HTTP请求的可能性?
有一个名为HTTPMiddleware的好项目可能很有用,它只是一个中间件框架,但效果很好.
这是一个代码示例:
import HTTP
import HTTPMiddleware
struct Middleware: HTTPRequestMiddlewareType {
func respond(request: HTTPRequest) -> HTTPRequestMiddlewareResult {
// You can change the request and pass it forward
return .Next(request)
// Or you can respond early and bypass the chain
return .Respond(HTTPResponse(statusCode: 404, reasonPhrase: "Not Found"))
}
}
struct Responder: HTTPResponderType {
func respond(request: HTTPRequest) -> HTTPResponse {
// May or may not be called depending on the middleware
return HTTPResponse(statusCode: 200, reasonPhrase: "OK")
}
}
let request = HTTPRequest(method: .GET, uri: URI(path: "/"))
let chain = Middleware() >>> Responder()
let response = chain.respond(request)
Run Code Online (Sandbox Code Playgroud)
这是官方页面,您还可以在源代码中找到对常见请求有用的JSON解析器.安装只需要uri_parser.
| 归档时间: |
|
| 查看次数: |
1697 次 |
| 最近记录: |