Home > Net >  get parameters in a get request in django rest framework?
get parameters in a get request in django rest framework?

Time:09-28

I want to get the parameters sent to my rest api

what I want is to obtain the parameters that to use them consume another api and return the response of the third party api but in name and comic i get None

http://127.0.0.1:8000/searchComics/

{name:"3-D Man","comics":12}

this is my view

class MarvelApi(APIView):
def get(self, request):
    private_key = "88958f2d87bd2c0c2fa07b7ea654bcdf9f0389b3"
    public_key = "8d415ffcc9add56b0a47c0a7c851afc3"
    ts = 1
    md5_hash = "46ecbbd63108b0561b8778a57823bd34"
    query_params = self.request.query_params
    name = query_params.get('kword', None)
    comic = query_params.get('comic', None)
    end_point = f"https://gateway.marvel.com:443/v1/public/characters?ts={ts}&apikey={public_key}&hash={md5_hash}&name={name}&comic={comic}"
    response = requests.get(end_point)
    response_json = json.loads(response.text)
    return Response(status=status.HTTP_200_OK, data=response_json)

I think the problem is these two lines

name = query_params.get('kword', None)

comic = query_params.get('comic', None)

that do not capture the values ​​correctly, do you know how to solve it?

CodePudding user response:

You wanted to get them from GET method, but instead you gave a dictionary, so I guess you sent it via POST. Instead of posting dictionary you should go with url:

http://127.0.0.1:8000/searchComics/?name=3-D Man&comic=12

And you had probably a typo. You had plural "comics" in dictionary and you seek for "comic" singular.

And if you want to have data with POST method, just change def get(...) to def post(...).

  • Related