0

Using Django and Django REST Framework, I am trying to write a GET request which when called will return a JSON file located on the server. What is the best way to accomplish this?

class MyView(APIView):

    def get(self, request):

        ...

        # I get a filepath to 'somefile.json'     
        my_filepath = "/Users/me/Desktop/somefile.json"

        ...

        # What do I do in order to return 'somefile.json'?    
        return Response(somefile.json)

EDIT

I do not want to return the contents of somefile.json. I want to return the file itself.

2
  • Does this help? Commented Feb 18, 2019 at 18:09
  • Yes, that is exactly what I was looking for! Thank you. return FileResponse(my_filepath, 'rb') did the trick Commented Feb 18, 2019 at 18:16

1 Answer 1

2

You can use a FileResponse object as described here.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.