Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- docker
- Filter
- function
- LiveData
- Generic
- collection
- react native
- recyclerview
- Kotlin
- Service
- list
- union
- Interface
- Foreign Key
- class component
- ReactNative
- ConstraintLayout
- lifecycle
- animation
- MINUS
- map
- CLASS
- elementAt
- mongoose
- vuex
- Swift
- 생명주기
- docker-compose
- enum
- AWS
Archives
- Today
- Total
개발 일기
Request Parsing ? 본문
Flask-RESTful에서는 기본적으로 reqParse라는 것을 통해서 요청 데이터에 접근할수 있습니다.
앞으로는 marshmallow을 사용한다고 합니다!.
기본적인 구성은 다음과 같습니다 :)
from flask_restful import reqparse
parser = reqparse.RequestParser()
parser.add_argument('rate', type=int, help='Rate cannot be converted')
parser.add_argument('name')
args = parser.parse_args()
#args["rate"] or args["name"]
여기서는 rate 와 name을 parser해서 딕셔너리형태로 사용할수 있습니다.
여기서 help란 Parsing중에 에러가 나면 Hint에 내용을 보여주도록 명시한것입니다.
만약에 하나의 Key에 대해서 여러개의 리스트로 받고 싶다면 다음과 같이 하면 됩니다.
parser.add_argument('name', action='append')
#{'name': ['email', 'email1', 'email2']}
만약에 파싱중에 매개변수 이름을 변경하고 싶을 때에는 dest 라는 키워드를 사용하면 됩니다.
parser.add_argument('name', dest='public_name')
#{'public_name': 'email'}
특정 위치에 값만 탐색을 하고 싶을 경우에는 다음과 같이 Location으로 작성을 할수가 있습니다.
# Look only in the POST body
parser.add_argument('name', type=int, location='form')
# Look only in the querystring
parser.add_argument('PageSize', type=int, location='args')
# From the request headers
parser.add_argument('User-Agent', location='headers')
# From http cookies
parser.add_argument('session_id', location='cookies')
# From file uploads
parser.add_argument('picture', type=werkzeug.datastructures.FileStorage, location='files')
# headers values에서 검색합니다.
parser.add_argument('text', location=['headers', 'values'])
부모로 부터 reqParse을 수정하고 싶을 때 다음과 같이 할수가 있습니다 :)
from flask_restful import reqparse
parser = reqparse.RequestParser()
parser.add_argument('foo', type=int)
parser_copy = parser.copy()
parser_copy.add_argument('bar', type=int)
# parser_copy has both 'foo' and 'bar'
parser_copy.replace_argument('foo', required=True, location='json')
# 'foo' is now a required str located in json, not an int as defined
# by original parser
parser_copy.remove_argument('foo')
# parser_copy no longer has 'foo' argument
위에 해당 내용처럼 제일 먼저 copy를 진행을 하고 그 다음에 bar을 받습니다. 그리고 기존에 foo가 있다면은 json 위치에 있는 값으로 교체를 합니다.
그럼다음에 삭제하고 싶을 때에는 remove_argument을 통해서 삭제할수 있습니다.
'서버 > Flask' 카테고리의 다른 글
Flask RestApi - (1) (0) | 2020.04.04 |
---|---|
Flask 기본 설치 방법 :) (0) | 2020.04.04 |
Comments