Home > Back-end >  Updating partial data in DB while fetching data should count as GET or PUT request in REST API
Updating partial data in DB while fetching data should count as GET or PUT request in REST API

Time:07-16

My class is to fetch current song info from Spotify api.

class GetCurrentSong(APIView):
    def get(self, request):

        dict_song_info = get_song_from_spotify(user_session=self.request.session.session_key)

        if 'Error' in dict_song_info:
            return Response({dict_song_info['Error_Type']: dict_song_info['Error']}, status=dict_song_info['Status'])     

        # Update song name in database
        try:
            self.update_song_info_in_db(dict_song_info['name'])
        except Exception as ex:
            return Response({'Storage Error': 'Caanot persist current song info to database'}, status=status.HTTP_406_NOT_ACCEPTABLE)

        return Response(dict_song_info, status=status.HTTP_200_OK)
    

Besides fetching song info and rendering to the frontend, I also need to update song name data in DB before rendering.

My question is: this function involves data update (no create new entries, just update existing data record). Does it still count as "Get"? Or actually, I should use "PUT" for this function?

CodePudding user response:

GET vs PUT is about the semantics of the request, not the details of the implementation.

If I'm asking you for the current copy of a web page, that's a GET, even if your implementation needs to download information from somewhere else and write into your own local cache/database.

  • Related