0

I have an upload url in my backend and i want to upload a file in another server.

My API view:

 class AssessmentFileUpload(APIView): parser_classes = (MultiPartParser, ) def post(self, request, format=None): tenant = request.user.tenant.id response = AssessmentFileUploadHelper(tenant).upload_file(request.FILES) response_text = json.loads(response.text) print(response_text) if response.status_code == status.HTTP_201_CREATED: return Response({"message": "success", "id": response_text.get('id')}, status=status.HTTP_201_CREATED) return Response({"message": "failed"}, status=status.HTTP_400_BAD_REQUEST) 

My class which sends request data to the other serve's url:

class AssessmentFileUploadHelper: def __init__(self, tenant_id): self.tenant_id = tenant_id def upload_file(self, file): print("FILE IS", file) url = settings.ASSESSMENT_CONNECTION_SETTINGS["api_endpoint"] + "tenant/" + \ str(self.tenant_id) + "/fileupload/" return RequestSender().send_request(url,None, file) class RequestSender: def __init__(self): super().__init__() def __get_authorized_header(self): usernamepassword = settings.ASSESSMENT_CONNECTION_SETTINGS["userid"] + ":" + settings.ASSESSMENT_CONNECTION_SETTINGS["password"] userAndPass = b64encode(usernamepassword.encode("utf-8")).decode("ascii") authorization = "Basic " + userAndPass headers = {'Authorization': authorization, "Content-Type": "application/json"} return headers def send_request(self, url, data, files=None): json_data = json.dumps(data) response = requests.post(url, data=json_data, headers=self.__get_authorized_header(), files=files ) return response 

Now, the errors im getting is InMemoryUploadedFile is not json serilizaable . How to send request.FILES to that server ?

11
  • Can you write the output of print(response.text) ? The value seems not to be a valid json structure. Commented Sep 29, 2020 at 8:06
  • response is not there because it throws error Commented Sep 29, 2020 at 8:14
  • The same error that you mentioned ? How about value of print(response) ? Commented Sep 29, 2020 at 8:16
  • i will update my question Commented Sep 29, 2020 at 8:19
  • 1
    Got it. Try seeing what is written in the request variable (post function). Commented Sep 29, 2020 at 8:33

1 Answer 1

1

You neet to convert 'InMemoryUploadedFile' type to string:

str = request.FILES['file'].read().decode() 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.