S
S
Sergey Beloventsev2017-10-05 09:28:29
Yii
Sergey Beloventsev, 2017-10-05 09:28:29

What am I doing wrong with rest and post request?

I configure the rest like this
config

[
                    'class' => 'yii\rest\UrlRule',
                    'controller' => 'v1/bonus',
                    'extraPatterns' => [
                    'POST bonus'=>'get-bonus'
       ],
 ],

I create the actionGetBonus action in the BunusController controller here is the controller
public function actionGetBonus(){
        $request = Yii::$app->request->post();
        $user=User::findOne(['ref'=>$request['ref']]);
        return [
            'full_bonus'=>$user->user_bonus,
            'frozen'=>$user->frozen,
            'bonus'=>($user->user_bonus-$user->frozen)
        ];  
    }

here is acController
here is what postman outputs
59d5cf1c38bb9129365957.jpeg
here is configured request using httpclient
$client = new Client();
            $response = $client->createRequest()
                ->setMethod('post')
                ->setUrl(\Yii::$app->params['bunusapi'])
                ->setData(['ref' => $userCode])
                ->send();
            if ($response->isOk) {
                $newUserId = $response->data['id'];
            }  else {
                return $response;
            }
        }

I get this answer
object(yii\httpclient\Response)[181]
  public 'client' => 
   ...
      public 'contentLoggingMaxSize' => int 2000
 ...
  private '_content' (yii\httpclient\Message) => string '{
    "name": "Unauthorized",
    "message": "Your request was made with invalid credentials.",
    "code": 0,
    "status": 401,
    "type": "yii\\web\\UnauthorizedHttpException"
}' (length=181)
...

(I just removed all parameters with null and empty. What am I doing wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Fedorov, 2017-10-05
@Sergalas

The problem is that you are trying to make a request without logging in. Accordingly, it is necessary to carry out the authentication process

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question