简体   繁体   中英

Microsoft graph Unable to read JSON request payload

I'm trying to create subscriptions with microsoft graph in php, however I am unable to see what is going wrong at this point.

The code is breaking at the following:

protected $http_subscribe = "https://graph.microsoft.com/v1.0/subscriptions";

public function getSubscriptions()
{
    if(empty($this->token)) {
        dd('no token supplied'); //some debugging
    }

    $date = $this->endTimeDate->format('Y-m-d'); //This is Carbon date

    $time = $this->endTimeDate->format('H:i:s.u');

    $response = $this->client->request('POST', $this->http_subscribe, [
        'headers' => [
            'Authorization' => 'Bearer ' . $this->token,
            'Content-Type' => "application/json"
        ], 'form_params' => [
            "changeType" => "created,updated",
            "notificationUrl" => "https://website.test/notify",
            "resource" => "me/mailFolders('Inbox')/messages",
            "expirationDateTime" => $date.'T'.$time,
            "clientState" => "secretClientValue"
        ]
    ]);

    dd($response);
}

The full error I'm getting is:

"""
{\r\n
  "error": {\r\n
    "code": "BadRequest",\r\n
    "message": "Unable to read JSON request payload. Please ensure Content-Type header is set and payload is of valid JSON format.",\r\n
    "innerError": {\r\n
      "request-id": "063d9947-80fd-461d-a70e-q0bd8eee9d56",\r\n
      "date": "2018-08-07T08:20:54"\r\n
   }\r\n
  }\r\n
}
"""

Now I get what the error says, my json would be invalid, however this array is in correct json format, I'm using Guzzle as my Client.

You should use json option. Try this:

$response = $this->client->request('POST', $this->http_subscribe, [
   'headers' => [
       'Authorization' => 'Bearer ' . $this->token,
   ],
   'json' => [
       "changeType" => "created,updated",
       "notificationUrl" => "https://website.test/notify",
       "resource" => "me/mailFolders('Inbox')/messages",
       "expirationDateTime" => $date.'T'.$time,
       "clientState" => "secretClientValue",
   ],
]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM