Welcome to the documentation page for LupinusHTTP!
LupinusHTTP is a lightweight HTTP networking library for iOS and macOS applications. It provides a simple interface for performing HTTP requests, handling responses, and managing cookies. Whether you are building a simple REST client or a complex networked application, LupinusHTTP has you covered.
Installation
Step 1: CocoaPods
- Open your terminal and navigate to the project directory.
- Create a Podfile by running:
pod init
- Open the Podfile and add the following line to your target:
pod 'LupinusHTTP'
- Save the file and run the following command:
pod install
- Open the generated .xcworkspace file and you’re ready to use LupinusHTTP.
Step 2: Manual Installation
- Download the latest version of LupinusHTTP from the GitHub repository.
- Unzip the downloaded file and locate the LupinusHTTP.framework file.
- Drag and drop the LupinusHTTP.framework file into your Xcode project.
- In Xcode, go to your project’s settings and select the target.
- Under Build Phases, expand the Link Binary With Libraries section.
- Click the ‘+’ button and add the LupinusHTTP.framework file.
- Build your project and you’re ready to use LupinusHTTP.
Getting Started
Creating an HTTP Request
- Import the LupinusHTTP module in your source file:
import LupinusHTTP
- Create an instance of HTTPRequest and set the required properties like URL and HTTP method:
“`swift
let request = HTTPRequest(url: URL(string: “https://example.com”)!)
request.method = .get
“`
Sending an HTTP Request
- Use the `send` method of the `HTTPClient` instance to send the request:
“`swift
let client = HTTPClient()
client.send(request) { (response, error) in
// Handle the response or error here
}
“`
Handling Response
Response Properties
- statusCode: The HTTP status code returned by the server.
- headers: The HTTP headers in the response.
- body: The raw body data of the response.
- text: The body of the response as a string.
- json: The body of the response as a JSON object (if applicable).
Response Error
- If the server returns an error status code (>= 400), the response’s `statusCode` property will reflect the error.
Managing Cookies
Storing Cookies
- Use the `HTTPCookieStorage.shared` instance to store cookies:
“`swift
let cookie = HTTPCookie(properties: [
.name: “SESSION”,
.value: “1234567890”,
.domain: “example.com”,
.path: “/”
])
HTTPCookieStorage.shared.setCookie(cookie)
“`
Getting Cookies
- Retrieve cookies for a specific URL:
“`swift
let cookies = HTTPCookieStorage.shared.cookies(for: URL(string: “https://example.com”)!)
“`
Deleting Cookies
- Remove cookies from the storage:
“`swift
let cookies = HTTPCookieStorage.shared.cookies(for: URL(string: “https://example.com”)!)
if let cookies = cookies {
for cookie in cookies {
HTTPCookieStorage.shared.deleteCookie(cookie)
}
}
“`
Conclusion
Congratulations! You have successfully integrated LupinusHTTP into your project. Feel free to explore the various functionalities it offers and leverage its power to build robust networking capabilities within your applications.
If you have any questions or encounter any issues, please refer to the official GitHub repository for additional documentation and support.
Happy coding!