0

My client uses the requests library to make this call to my Django server

import requests
response = requests.post(url, files=dict({
            'value': 'key',
        }))

This will create a requests that inserts the dictionary into the field request.FILES as a <MultiValueDict: {}>

I am trying to recreate this with django.test.
I keep seeing to try something like

from django.test import TestCase, Client
client = Client()
response = client.post('/sign', dict(request_data))

but the request.FILES object is empty

edit ---- I have also tried with the same result ( request.FILES -> <MultiValueDict: {}>)

client.post('/sign', {'file': dict({
  'key' : 'value'
})})

Edit 2---

A look at the midldleware where I am checking the value


class ApiAuthenticationMiddleware:
    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request: HttpRequest):
        print(request.FILES)
        

1 Answers1

-1

Solution

        fake_file.name = 'data.json'
        post_data = {
            'data.json': fake_file,
        }
        return self.client.post('/sign', post_data,  content_type=MULTIPART_CONTENT)