[Error] type_error.dict : value is not a valid dict

JUNG정·2024년 12월 10일

backend

목록 보기
8/13

오류

오류 메시지 "422 Error: Unprocessable Entity"는 서버가 요청을 이해했지만, 요청된 내용에 문제가 있어 처리할 수 없음을 나타냅니다.

Requset body

{
  "format": "png",
  "mask_image": "string" / 테스트때문에 추가 또는 삭제하느라고 오류 캡쳐할때 있다가 없다가 할 수 있음
}

Response body

{
  "detail": [
    {
      "loc": [
        "body",
        "request_param"
      ],
      "msg": "value is not a valid dict",
      "type": "type_error.dict"
    }
  ]

Curl

curl -X 'POST' \
  'http://127.0.0.1:8080/주소/주소' \
  -H 'accept: application/json' \
  -H 'Content-Type: multipart/form-data' \
  -F 'file=@파일명.확장자;type=MIME타입' \
  -F 'request_param={
        "format": "png",
        "mask_image": "string"
	}'



원인

파일 업로드와 requset_param을 같이 받으면 오류가 발생한다
문자열로 보내진 데이터를 Pydantic에서 json으로 받는걸로 작성 했기 때문에 오류 발생

  • 업로드 파일(UploadFile) : Content-Type: multipart/form-data
  • 클래스 타입 데이터(request_param) : Content-Type: application/json

request_param을 class type으로 지정해서 json형태로 받도록 코드를 작성했음

 async def 함수명(self, file: UploadFile, request_param: models.RequestsClassName, 일부생략 ):
class RequestsClassName(Base):
    __tablename__ = 'db_name'

    format = Column(String(45))
    mask_image = Column(String(255))

curl 요청 Content-Type 을 확인해보면 multipart/form-data 로 보낸 경우 자동으로 JSON 객체로 변환하지 않고 문자열 그대로 받게 되는거임

그래서 Pydantic 모델로의 변환이 실패하며 type이 맞지 않아서 "value is not a valid dict"라는 오류가 발생한거임



해결

이전에는 RequestsClassName 타입을 사용하여 JSON 객체를 받는것이 아닌,

Content-Type을 multipart/form-data를 사용하여 데이터를 전송하는 것으로 수정.

 async def 함수명(self, file: UploadFile, format:Optional[str], mask_image:Optional[str], 일부생략):
profile
기록하는 습관

0개의 댓글