ben_01's avatar

Lumen data validation fails

I'm validating data sent via Guzzle post request but validation fails. I've logged the data and i can see that it exists but validation rule exists fails. Basically i'm trying to send data from one lumen project to another using a Guzzle POST request.

This class has code to send data

<?php
public function sendData() {
    $user_id = 123;
    $email = '[email protected]';
    $data = ['userId' => $user_id, 'email' => $email];
    $this->guzzlePost('API TO RECEIVE THE DATA', $data); // From Guzzle trait
}

This code is a different lumen project to receive data

<?php
public function receiveData(Request $request) {
    $validator = Validator::make($request->all(), [
            'user_id' => 'required|exists:myuser_table,id',
            'email' => 'required|unique:myuser_table,email'
            ]);

    if ($validator->fails()) {
            throw new \Illuminate\Validation\ValidationException($validator);
        } else {
        return 'Data received';
        }
}
0 likes
7 replies
Robstar's avatar

userId in first post snippet should be user_id

ben_01's avatar

Sorry . Here is the updated first snippet

<?php
public function sendData() {
    $user_id = 123;
    $email = '[email protected]';
    $data = ['user_id' => $user_id, 'email' => $email];
    $this->guzzlePost('API TO RECEIVE THE DATA', $data); // From Guzzle trait
}
Robstar's avatar

I guess the next obvious questions are:

  • is the request object you're injecting a custom request, or the default Laravel request instance
  • what are the ontents of the guzzlePost method?
  • what data is actually arriving at your endpoint? Posts the contents here
sujancse's avatar

So here you are checking user_id against myuser_table's id, Is it id or user_id in your myuser_table @ben_01?

ben_01's avatar
  1. The request is the default laravel Request instance.
  2. Guzzle post request has the following content. [2019-12-19 11:26:21] local.NOTICE: {"user_id":123,"email":"[email protected]"}
  3. On the endpoint receiving the data, the content of $request->all() is as follows:
(
    [user_id] => 123
    [email] => "[email protected]"
)```
sujancse's avatar

I am asking for table column name is it id or user_id on myuser_table @ben_01 ?

ben_01's avatar

I'm embarrassed to say that user_id data is sent as int but i think guzzle converted it to string and that's why validation was failing.

Please or to participate in this conversation.