본문 바로가기

API9

Python 플라스크에서 POST된 JSON을 어떻게 받을 수 있나요?, How to get POSTed JSON in Flask? 질문 간단한 Flask API를 구축하려고 합니다. 이 API에서는 POST된 JSON을 읽고 싶습니다. POST는 Postman Chrome 확장 프로그램을 사용하여 수행하며, POST된 JSON은 단순히 {"text":"lalala"}입니다. 다음 방법을 사용하여 JSON을 읽으려고 합니다: @app.route('/api/add_message/', methods=['GET', 'POST']) def add_message(uuid): content = request.json print content return uuid 브라우저에서는 올바르게 GET에 입력한 UUID를 반환하지만 콘솔에서는 None만 출력됩니다. (예상대로 {"text":"lalala"}를 출력해야 합니다). Flask 메소드 내에서 .. 2023. 10. 24.
Python 싱글톤을 정의하는 간단하고 우아한 방법이 있을까요? [중복됨], Is there a simple, elegant way to define singletons? [duplicate] 질문 파이썬에서 싱글톤을 정의하는 다양한 방법이 있는 것 같습니다. Stack Overflow에서는 합의된 의견이 있을까요? 답변 I don't really see the need, as a module with functions (and not a class) would serve well as a singleton. All its variables would be bound to the module, which could not be instantiated repeatedly anyway. If you do wish to use a class, there is no way of creating private classes or private constructors in Python, so you ca.. 2023. 10. 12.
Python 리스트 vs 튜플, 각각 언제 사용해야 하나요? [중복], List vs tuple, when to use each? [duplicate] 질문 파이썬에서는 언제 리스트를 사용하고 언제 튜플을 사용해야 할까요? 가끔 선택의 여지가 없을 때도 있습니다. 예를 들어 "hello %s you are %s years old" % x 이렇게 할 경우 x는 튜플이어야 합니다. 하지만 API를 디자인하고 데이터 유형을 선택할 수 있는 사람이라면 가이드라인은 무엇일까요? 답변 튜플은 고정 크기입니다. 반면에 리스트는 동적입니다. 다른 말로, 튜플은 불변이고 리스트는 가변입니다. 튜플에 요소를 추가할 수 없습니다. 튜플에는 append나 extend 메서드가 없습니다. 튜플에서 요소를 제거할 수 없습니다. 튜플에는 remove나 pop 메서드가 없습니다. 튜플에서 요소를 찾을 수 있습니다. 이는 튜플을 변경하지 않기 때문입니다. in 연산자를 사용하여 튜플.. 2023. 10. 9.
Flutter request.send()를 사용하여 응답 본문을 어떻게 가져올 수 있나요?, How to get response body with request.send() in dart 질문 I'm doing an api request uploading an image with var request = new http.MultipartRequest("POST", uri); var response = await request.send() in dart using flutter. I can then check the response code with for instance if (response.statusCode == 200) { print('ok'); } with other calls I can also get the response body with var result = response.body; however when using request.send() I can't seem t.. 2023. 8. 16.