Alamofire?

Alamofire는 swift를 기반으로 한 HTTP 네트워킹 라이브러리이다.
Foundation의 URLSession 기반으로 구현되어 있어, 더 쉽게 사용할 수 있도록 한다.
즉, Alamofire는 HTTP 네트워킹을 하는데 있어서 자주 사용하는 코드 및 함수를 쉽게 사용할 수 있도록 한다!

Request Methods

  • 클라이언트가 서버에게 사용자 요청의 목적이나 종류를 알리는 수단

서버의 여러 정보를 확인하기 위해 사용되는 메소드이다.
Response Body가 없고, Response Code와 HEAD만 응답받는다.

HEAD / index.html

GET

서버의 데이터를 가져올 때 사용하는 메소드이다.
Request Body가 없기때문에 전달할 내용을 header에 담는다.

GET / index.html

POST

서버로 데이터를 전송할 때 사용하는 메소드로, 요청 유형은 Content-Type 헤더로 나타낸다.
전달할 내용을 Request Body에 담는다.

POST / HTTP/1.1
Host: foo.com
Content-Type: application/x-www-form-urlencoded
Content-Length: 13

say=Hi&to=Mom

PUT

새로운 리소스를 생성하거나, 기존 리소스를 변경할 때 사용하는 메소드이다.

PUT /new.html HTTP/1.1
Host: example.com
Content-type: text/html
Content-length: 16

<p>New File</p>

DELETE

서버에 있는 특정 리소스를 삭제하기 위해 사용하는 메소드이다.

DELETE /file.html HTTP/1.1

GET Method

let url = "https://www.p2glet.com"

AF.request(url,
		   method: .get,
    	   parameters: nil,
    	   encoding: URLEncoding.default,
    	   headers: ["Content-Type":"application/json", "Accept":"application/json"])
	.validate(statusCode: 200..<300)
    .responseJSON { response in
    	
    switch response.result {
    case .success(let data):
		// success
	case .failure(let error):
    	// failure
	}
}

url: 첫 번째 파라미터로 요청할 url을 담는다. method: 어떤 request method를 사용할 것 인지를 나타낸다. parameters: POST 메소드와 같이 request body를 사용할 때 전달할 값을 담는다. encoding: 인코딩 방식을 지정한다. headers: 부가적인 정보를 나타낸다. 위에서는 송/수신 데이터 타입을 나타낸다. validate: 유효성을 검사한다. responseJSON: 응답 json 데이터이다.

댓글남기기