tastyigniter / ti-ext-api

Manages and generates RESTful APIs for TastyIgniter
MIT License
17 stars 22 forks source link

Error POST /api/customers #41

Closed devexsolutions closed 3 years ago

devexsolutions commented 3 years ago

Expected behavior:

Status: 201 Created
{
  "data": [
    {
      "type": "customers",
      "id": "1",
      "attributes": {
        "first_name": "Joe",
        "last_name": "Bloggs",
        "email": "joe@bloggs.com",
        "telephone": "1234512345",
        "newsletter": false,
        "customer_group_id": 1,
        "date_added": "2020-05-20 08:34:37",
        "status": true,
        "full_name": "Joe Bloggs",
        "addresses": [
          {
            "address_id": 1,
            "customer_id": 1,
            "address_1": "1 Some Road",
            "address_2": null,
            "city": "London",
            "state": "",
            "postcode": "W1A 3NN",
            "country_id": 222
          }
        ]
      }
    }
  ]
}

Actual behavior:

{
    "message": "Call to undefined method Igniter\\Api\\ApiResources\\Customers::getFormModel()",
    "status_code": 500,
    "debug": {
        "line": 445,
        "file": "C:\\xampp4\\htdocs\\bares\\vendor\\tastyigniter\\flame\\src\\Traits\\ExtendableTrait.php",
        "class": "BadMethodCallException",
        "trace": [
            "#0 C:\\xampp4\\htdocs\\bares\\vendor\\tastyigniter\\flame\\src\\Support\\Extendable.php(42): Igniter\\Flame\\Support\\Extendable->extendableCall('getFormModel', Array)",
            "#1 C:\\xampp4\\htdocs\\bares\\vendor\\laravel\\framework\\src\\Illuminate\\Support\\Optional.php(127): Igniter\\Flame\\Support\\Extendable->__call('getFormModel', Array)",
            "#2 C:\\xampp4\\htdocs\\bares\\app\\system\\classes\\FormRequest.php(106): Illuminate\\Support\\Optional->__call('getFormModel', Array)",
            "#3 C:\\xampp4\\htdocs\\bares\\app\\admin\\requests\\Customer.php(31): System\\Classes\\FormRequest->getModel()",
            "#4 C:\\xampp4\\htdocs\\bares\\vendor\\laravel\\framework\\src\\Illuminate\\Container\\BoundMethod.php(36): Admin\\Requests\\Customer->rules()",
            "#5 C:\\xampp4\\htdocs\\bares\\vendor\\laravel\\framework\\src\\Illuminate\\Container\\Util.php(37): Illuminate\\Container\\BoundMethod::Illuminate\\Container\\{closure}()",
            "#6 C:\\xampp4\\htdocs\\bares\\vendor\\laravel\\framework\\src\\Illuminate\\Container\\BoundMethod.php(93): Illuminate\\Container\\Util::unwrapIfClosure(Object(Closure))",
            "#7 C:\\xampp4\\htdocs\\bares\\vendor\\laravel\\framework\\src\\Illuminate\\Container\\BoundMethod.php(37): Illuminate\\Container\\BoundMethod::callBoundMethod(Object(Igniter\\Flame\\Foundation\\Application), Array, Object(Closure))",
            "#8 C:\\xampp4\\htdocs\\bares\\vendor\\laravel\\framework\\src\\Illuminate\\Container\\Container.php(590): Illuminate\\Container\\BoundMethod::call(Object(Igniter\\Flame\\Foundation\\Application), Array, Array, NULL)",
            "#9 C:\\xampp4\\htdocs\\bares\\app\\system\\classes\\FormRequest.php(117): Illuminate\\Container\\Container->call(Array)",
            "#10 C:\\xampp4\\htdocs\\bares\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\FormRequest.php(84): System\\Classes\\FormRequest->createDefaultValidator(Object(Igniter\\Flame\\Validation\\Factory))",

Reproduce steps: I am trying to create a client from the API, following the extension documentation. But I get an error when I make the call to the EndPoint. First I get a new Token and then what I do is add the parameters as seen in the attached code (Curl). For testing, I am using PostMan

` **$curl = curl_init();

curl_setopt_array($curl, array( CURLOPT_URL => 'http://localhost/bares/api/customers?first_name=Jon&last_name=Doe&email=ja_camseg@hotmail.com&telephone=1234512345', CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => '', CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 0, CURLOPT_FOLLOWLOCATION => true, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => 'POST', CURLOPT_HTTPHEADER => array( 'Authorization: Bearer 1|nyCzahZn3qz6ezG3W4y88Y0LIheypzy12JDefhvO3hkgGBlyLUnqSL6FYsT8ZeDsXgHD9HU0iMSDMJV1' ), ));

$response = curl_exec($curl);

curl_close($curl); echo $response;**`

Version: 3.0.4 beta 27

Additional Information: (Additional information, if any)

ryanmitchell commented 3 years ago

I’m assuming you’re using the latest GitHub main/master code against the latest beta of core?

there are changes in core that need to be tracked in - specifically related to getrestmodel. I’m on my phone so can’t find it to link to.

Beyond that you are using a GET request when it should be POST. You should be passing form data not url parameters.

devexsolutions commented 3 years ago

Yes, I am using the latest code uploaded to the repository of both TI and TI-EXT-API project. At what point am I using a GET request instead of a POST request? According to the documentation POST api / customers, is used to create a customer. Thanks

ryanmitchell commented 3 years ago
$curl = curl_init();

curl_setopt_array($curl, array(
CURLOPT_URL => 'http://localhost/bares/api/customers',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_HTTPHEADER => array(
'Authorization: Bearer 1|nyCzahZn3qz6ezG3W4y88Y0LIheypzy12JDefhvO3hkgGBlyLUnqSL6FYsT8ZeDsXgHD9HU0iMSDMJV1'
),
CURLOPT_POSTFIELDS => json_encode([
'first_name' => 'x',
'last_name' => 'y',
]),
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;**

Think you also are missing that commit: https://github.com/tastyigniter/TastyIgniter/commit/a4392380366e0529696f0ac03931e7a6a3dece4b

devexsolutions commented 3 years ago

Now yes friend, added the changes to FromRequest.php and it works fine. Thanks.