簡體   English   中英

通過 Postman 發送 post 請求有效,但在使用 http 時無效

[英]Sending post request through Postman works but not when using http

我正在將圖像上傳到我的服務器(現在它只是本地主機)。 下面是我的views.py中導致問題的代碼部分。

valid_extensions = ['jpg', 'jpeg', 'png', 'gif']
images = request.data.getlist('images[]') #Error here I believe
directory = os.path.join(os.getcwd(), 'static', 'images', 'products')

for image in images:
    extension = os.path.splitext(image.name)[1][1:].lower()
    file_name = "".join(choice(ascii_lowercase) for i in range(16)) + "." + extension

    if extension in valid_extensions:
        file_path = os.path.join(directory, file_name)
        with open(file_path, 'wb+') as destination:
            for chunk in image.chunks():
                destination.write(chunk)
                ProductImage.objects.create(file_name=file_name, original_name=image.name, file_length=image.size, product=product, file_path=file_path.replace(os.getcwd(), '').replace('\\', '/'))
    else:
        data = {'full_messages': ['Image file type is not supported.']}
        return Response(data, status=status.HTTP_422_UNPROCESSABLE_ENTITY)

data = {'full_messages': ['Product created successfully.']}
return Response(data, status=status.HTTP_201_CREATED)

出於某種原因,當我通過 Postman 發送我的 post 請求時,它成功上傳了文件,但是當我通過 HTTP (Angular 8) 發送相同的請求時,我在控制台中收到錯誤消息。

它說AttributeError: 'NoneType' object has no attribute 'decode'如果我改變了

以下是從 http 發送時的請求從 http 發送時的請求

以下是郵遞員請求郵遞員請求

編輯(包括回溯)

Traceback (most recent call last):
  File "/usr/local/Cellar/python/3.7.4_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/wsgiref/handlers.py", line 137, in run
    self.result = application(self.environ, self.start_response)
  File "/Users/akwild/Desktop/project1/backend/env/lib/python3.7/site-packages/django/contrib/staticfiles/handlers.py", line 65, in __call__
    return self.application(environ, start_response)
  File "/Users/akwild/Desktop/project1/backend/env/lib/python3.7/site-packages/django/core/handlers/wsgi.py", line 141, in __call__
    response = self.get_response(request)
  File "/Users/akwild/Desktop/project1/backend/env/lib/python3.7/site-packages/django/core/handlers/base.py", line 75, in get_response
    response = self._middleware_chain(request)
  File "/Users/akwild/Desktop/project1/backend/env/lib/python3.7/site-packages/django/core/handlers/exception.py", line 36, in inner
    response = response_for_exception(request, exc)
  File "/Users/akwild/Desktop/project1/backend/env/lib/python3.7/site-packages/django/core/handlers/exception.py", line 90, in response_for_exception
    response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())
  File "/Users/akwild/Desktop/project1/backend/env/lib/python3.7/site-packages/django/core/handlers/exception.py", line 125, in handle_uncaught_exception
    return debug.technical_500_response(request, *exc_info)
  File "/Users/akwild/Desktop/project1/backend/env/lib/python3.7/site-packages/django/views/debug.py", line 91, in technical_500_response
    text = reporter.get_traceback_text()
  File "/Users/akwild/Desktop/project1/backend/env/lib/python3.7/site-packages/django/views/debug.py", line 340, in get_traceback_text
    c = Context(self.get_traceback_data(), autoescape=False, use_l10n=False)
  File "/Users/akwild/Desktop/project1/backend/env/lib/python3.7/site-packages/django/views/debug.py", line 305, in get_traceback_data
    'filtered_POST_items': list(self.filter.get_post_parameters(self.request).items()),
  File "/Users/akwild/Desktop/project1/backend/env/lib/python3.7/site-packages/django/views/debug.py", line 177, in get_post_parameters
    return request.POST
  File "/Users/akwild/Desktop/project1/backend/env/lib/python3.7/site-packages/django/core/handlers/wsgi.py", line 110, in _get_post
    self._load_post_and_files()
  File "/Users/akwild/Desktop/project1/backend/env/lib/python3.7/site-packages/django/http/request.py", line 315, in _load_post_and_files
    self._post, self._files = self.parse_file_upload(self.META, data)
  File "/Users/akwild/Desktop/project1/backend/env/lib/python3.7/site-packages/django/http/request.py", line 274, in parse_file_upload
    parser = MultiPartParser(META, post_data, self.upload_handlers, self.encoding)
  File "/Users/akwild/Desktop/project1/backend/env/lib/python3.7/site-packages/django/http/multipartparser.py", line 72, in __init__
    raise MultiPartParserError('Invalid boundary in multipart: %s' % boundary.decode())
AttributeError: 'NoneType' object has no attribute 'decode'

您應該在標題中添加 enctype 而不是 Content-Type。 這對我有用。

 headers: new HttpHeaders({
        'enctype': 'multipart/form-data',
        'Authorization': 'Token ' + user.token
      })

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM