Sinatra를 이용해서 웹 애플리케이션을 개발하던 중, 클라이언트의 요청을 다른 웹서버로 보내야 하는 API를 만들게 됐다. 그냥 GET 호출만 할게 아니고, JSON 형태의 값을 POST로 넘기고, 거기에 HTTP header까지 제어해야 했다. 해서 꽤 많은 일을 할 수 있는 HTTP client api를 찾아야 했다.
그렇게 찾은게 NET::HTTP다. 몇개 더 있는 것 같기는한데, 대략 내가 원하는 일들은 다 할 수 있을 것 같아서 그냥 선택하기로 했다.
URI
HTTP 요청은 꽤 많은 구성요소를 가진다. "프로토콜://Domain:port/PATH?uriquery#fragment"의 구성요소를 가지는데, 이것을 다루는 것도 꽤나 귀찮은 일이다. ruby에서 제공하는 uri 모듈을 사용하기로 하자.
Contents
NET::HTTP
URI
require 'uri' uri = URI("http://www.joinc.co.kr/wiki.php/test?action=edit#sect1") puts uri.scheme # http puts uri.host # www.joinc.co.kr puts uri.port # 80 puts uri.path # wiki.php/test puts uri.query # action=edit puts uri.fragment # sect1 puts uri.to_s # http://www.joinc.co.kr/wiki.php/test?action=edit#sect1테스트를 위한 PHP 코드
GET 요청 작성
require 'net/http' # -*- coding: utf-8 -*- require 'uri' require 'net/http' url = URI.parse("http://www.joinc.co.kr/test.php?name=yundream&key=value") http = Net::HTTP.new(url.host, url.port) req = Net::HTTP::Get.new(url.request_uri) req.add_field("MY-Header", "1234567890") response = http.request(req) puts response.body puts response.codeArray ( [SERVER_ADDR] => xx.xx.xxx.xx [SERVER_PORT] => 80 [REMOTE_ADDR] => xx.xxx.xxx.xx [REMOTE_PORT] => 54486 [GATEWAY_INTERFACE] => CGI/1.1 [SERVER_PROTOCOL] => HTTP/1.1 [REQUEST_METHOD] => GET [QUERY_STRING] => name=yundream&key=value [REQUEST_URI] => /test.php?name=yundream&key=value ) Array ( [Accept] => */* [User-Agent] => Ruby [My-Header] => 1234567890 [Connection] => close [Host] => www.joinc.co.kr ) 200POST 요청 작성
#!/usr/bin/ruby # -*- coding: utf-8 -*- require 'uri' require 'net/http' url = URI.parse("http://www.joinc.co.kr/test.php") http = Net::HTTP.new(url.host, url.port) req = Net::HTTP::Post.new(url.request_uri) req.add_field("MY-Header", "1234567890") req.add_field("Content-type", "application/json") req.body = "hello world" response = http.request(req) puts response.body puts response.codeArray ( [SERVER_ADDR] => xx.xxx.xx.xx [SERVER_PORT] => 80 [REMOTE_ADDR] => xx.xxx.xx.xx [REMOTE_PORT] => 54477 [GATEWAY_INTERFACE] => CGI/1.1 [SERVER_PROTOCOL] => HTTP/1.1 [REQUEST_METHOD] => POST [REQUEST_URI] => /test.php ) Array ( [Accept] => */* [User-Agent] => Ruby [My-Header] => 1234567890 [Content-Type] => application/json [Connection] => close [Host] => www.joinc.co.kr [Content-Length] => 11 ) hello world 200HTTPS 연결
응답 정보 가져오기
- body() 메서드를 이용해서, HTTP Body 정보를 가져올 수 있다.
- code 어트리뷰트를 이용해서, HTTP Code를 가져올 수 있다.
HTTP Header는 Net::HTTPHeader 로 가져올 수 있다.res.get_fields('Set-Cookie') # ["JSESSIONID=1234895011ace; Path=/index.php; Secure"]Recent Posts
Archive Posts
Tags