API - postman을 이용한 API 요청 방법

  • 이번에는 postman을 사용하여 클라이언트와 서버간 API 요청 방법에 대해 알아볼 것이다.

postman 사용하여 API 요청하기

1. 좌측 Collection에 프로젝트(drf-yasg) 이름으로 폴더 생성

Screenshot from 2019-07-22 02-41-07

2. 해당 프로젝트 폴더 안에 앱(blog) 이름으로 폴더 생성

Screenshot from 2019-07-22 02-41-18

3. GET 방식으로 ‘도메인 주소’/api/blog/posts/ 설정 이후, 우측 Save as 클릭하여 ‘Post List’로 명명

Screenshot from 2019-07-22 02-41-51

4. Authorization에는 Basic Auth 선택하여 username 및 password 입력

Screenshot from 2019-07-22 02-42-36

  • DB에 저장되어 DB 삭제되면 다시 token을 발급해야 하는 Token Auth와는 달리 Basic Auth는 한번 설정해놓으면 다시 설정할 필요가 없다.

5. Post List 에서 api 요청시 아래 데이터 확인

Screenshot from 2019-07-22 02-44-00

6. POST 방식으로 ‘도메인 주소’/api/blog/posts/ 설정한 페이지를 ‘Post Create’로 명명 -> Basic Auth 설정 및 Body에 생성할 객체의 key, value 값 입력

Screenshot from 2019-07-22 02-43-50


추가) 프로젝트 API 문서 작성

  1. Github > 해당 프로젝트의 repository > Wiki 이동
    Screenshot from 2019-07-22 01-21-24

  2. 클라이언트에게 제공할 api 문서 작성

    BaseURL: http://<domain>/api/

    Post

    블로그 글

    Post List

    블로그 글 목록을 리턴

    • URL: /posts/
    • Method: GET

      Response

      1
      2
      3
      4
      5
      [
      {Post Object},
      {Post Object},
      {Post Object}
      ]
      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
      [
      {
      "title": "Post1",
      "content": "",
      "is_published": false,
      "pk": 1,
      "author": 1,
      "created": "2019-07-21T15:41:40.741196Z",
      "modified": "2019-07-21T15:41:40.741234Z"
      },
      {
      "title": "Post2",
      "content": "content2",
      "is_published": false,
      "pk": 2,
      "author": 1,
      "created": "2019-07-21T15:41:56.378422Z",
      "modified": "2019-07-21T15:41:56.378453Z"
      },
      {
      "title": "post3",
      "content": "content3",
      "is_published": false,
      "pk": 3,
      "author": 1,
      "created": "2019-07-21T15:42:07.168257Z",
      "modified": "2019-07-21T15:42:07.168305Z"
      }
      ]

      Post Create

      블로그 글을 작성

    • URL: /posts/

    • Method: POST
    • Authorization: required

      Request

    • title (String)
    • content (String)

      Response

      Structure

      1
      2
      3
      4
      5
      [
      {Post Object},
      {Post Object},
      {Post Object}
      ]

      Example
      `json
      {
      “title”: “PostTitle”,
      “content”: “PostContent”,
      “is_published”: false,
      “pk”: 4,
      “author”: 1,
      “created”: “2019-07-21T16:30:09.836537Z”,
      “modified”: “2019-07-21T16:30:09.836582Z”
      }