티스토리 뷰

workspace/python

python으로 REST API 호출

gl_shlee 2017. 1. 27. 21:23
반응형

python을 이용하여 간단한 REST client를 만들어 보자.


먼저 http request를 위해 사용할 라이브러리로 httplig2를 선택했다.

httplib2 : https://github.com/httplib2/httplib2


httplib2를 설치하는 방법은 아래와 같다.


$ pip install httplib2


설치를 완료했으면 간단하게 테스트를 해보자.

아래의 코드를 작성하고 실행해 보자.


import httplib2


http = httplib2.Http()

url = 'https://jsonplaceholder.typicode.com/posts/1'

response, content = http.request(url, 'GET')

print(content)


아래와 같은 content가 나오면 성공.


b'{\n  "userId": 1,\n  "id": 1,\n  "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",\n  "body": "quia et suscipit\\nsuscipit recusandae consequuntur expedita et cum\\nreprehenderit molestiae ut ut quas totam\\nnostrum rerum est autem sunt rem eveniet architecto"\n}'


반응형

'workspace > python' 카테고리의 다른 글

PyCharm 설치  (0) 2017.01.22
OSX - python3 설치  (0) 2017.01.22
OS X - python 개발 환경 구축  (0) 2016.07.07
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함