I have gone through answers of this type of question but they don’t seem relevant for my problem.
I have a class defined as follows:
class TrainModel(APIView): permission_classes = (IsAuthenticated,) def post(self, request, *args): print("I am here") params = json.loads(request.POST["params"]) print(params) #setup_instance.apply_async((params,), queue="local") fit_model(params, "") model_name = "{}-{}-{}-{}".format( params["match_slug"], params["match_id"], params["model_type"], params["version"]) response = { "success": True, "message": "Model is being trained please wait", "data": {"model_name": model_name} } return JsonResponse(response)
It also requires other inputs:
params["meta"]["xgb"]
params["combined"]
And I pass them as follows:
import requests import json headers = {'Authorization': 'Token $TOKEN', 'content-type': 'application/json'} url = 'http://somesite.com/train/' params = { "match_id": 14142, "model_type": "xgb", "version": "v2", "match_slug": "csk-vs-kkr-26-mar-2022", "meta": { "xgb": { "alpha": 15, "max_depth": 4, "objective": "reg:linear", "n_estimators": 53, "random_state": 0, "learning_rate": 0.1, "colsample_bytree": 0.4 }, "catboost": { "cat_cols": [ "batting order_lag_1", "venue", "against", "role", "bowlType" ], "eval_metric": "RMSE", "random_seed": 42, "logging_level": "Silent", "loss_function": "RMSE" } }, "image_id": "SOME_ID", "combined": 0 } resp = requests.post(url, params=params, headers=headers) print(resp)
I try the same in Postman by putting all these parameters in “Body” (using “raw”) but get:
Exception Type: MultiValueDictKeyError Exception Value: 'params'
Need help. Thanks.
Advertisement
Answer
class TrainModel(APIView): permission_classes = (IsAuthenticated,) def post(self, request): data = request.data # I would suggest using a serializer and it automatically validates your data fit_model(data, "") model_name = "{}-{}-{}-{}".format( data.get("match_slug"), data.get("match_id"), data.get("model_type"), data.get("version")) response = { "success": True, "message": "Model is being trained please wait", "data": {"model_name": model_name} } return Response(response, status=status.HTTP_200_OK)