Anteris-Dev / autotask-client

An HTTP client for the Autotask API built with PHP.
MIT License
23 stars 16 forks source link

create resource returns empty item id #81

Open Ash-raf10 opened 1 year ago

Ash-raf10 commented 1 year ago

Whenever I create a new resource/entity I am supposed to get the created resource/entity id in the response but instead I am getting empty({}) response. The response is showing 200 status. Also I checked, the record is indeed getting created in the autotask.

This is the code I am using

public function createContact()
{
    $contact = new ContactEntity(
        [
            'id' => 0, // Autotask requires that new entities have an ID of 0
            'companyID' => 0,
            'firstName' => 'New',
            'lastName' => 'User',
            'isActive' => 1,
        ]
    );
    try {
        $response = $this->autoTaskClient->contacts()->create($contact);
        return response()->json($response);
    } catch (\Exception $exception) {
        return $exception->getMessage();
    }
}

It returned {} only.

aidan-casey commented 1 year ago

@Ash-raf10 - What is the response on this same request using something like Postman?

miltechnz commented 1 month ago

Did you figure this out as I too am having the same issue. I am creating a ticket and then need to go and add time entries to the ticket but dont have a created entity ID.

miltechnz commented 1 month ago

PS. I figured this out. The "response" returned from the initial query is just the Guzzle stream. You need to call getBody() on the Guzzle response stream to get the actual server response in JSON.

(Credit to - https://stackoverflow.com/questions/30549226/guzzlehttp-how-get-the-body-of-a-response-from-guzzle-6)

So i.e. ... $response = $client->tickets()->create( $ticket ); $contents = (string) $response->getBody(); echo $contents; ...

$contents will contain a string of the raw json response (like {"itemId":10018}). Obviously not very helpful as a string but this was just for testing. Do with it as you please.

I believe this should be the same for any entity :).