oten
121
2
Laravel

Best approach to handle third party API responses

Posted 8 months ago by oten

What would be the method of choice for handling responses coming from a REST API?

For example consider the following response:

[
  "name": "John Doe",
  "username": "john",
  "posts": [
     0: => [
       "title" => "First Post",
       "body" => "This is the first post."
     ]
     1: => [
       "title" => "Second Post",
       "body" => "This is the second post."
     ]
  ]
]

Basically what I'm trying to do is to have a separate object for each post returned by the API. I assume that one way would be to create two classes (not Eloquent models since I don't want the response to be stored in my database) "User" and "Post" and loop through the array to create objects accordingly.

According to this article I could use Laravel Resources to consume the response but that doesn't seem to work in my case.

Please sign in or create an account to participate in this conversation.