0

Scrathing my head here. What is wrong with this code? The response from Expo is a 404 for some reason. I'm sending stuff to them in the wrong way but can't figure out how I should do it!.

EXPO docs are here: https://docs.expo.io/versions/latest/guides/push-notifications#http2-api

My code:

$client = new Client(); //GuzzleHttp\Client

    $url = 'https://exp.host/--/api/v2/push/send';

    $data = [
        'to' => array('ExponentPushToken[gaXXXXXXXXX_Oi4J1b9OR]'),
        'title' => $notification->title,
        'body' => $notification->body
    ];
    $headers = [
        'Accept' => 'application/json',
        'Accept-Encoding' => 'gzip, deflate',
        'Content-Type' => 'application/json',
    ];

    $response = $client->post($url, ['form_params' => $data, 'headers' => $headers]);


    dd($response);

Getting the following error:

Client error: `POST https://exp.host/--/api/v2/push/send` resulted in a `404 Not Found` response: Not Found

NOTE: Changing 'form_params' to 'multipart' inte the guzzle post gives me a different error from expo:

A 'contents' key is required

And finally, changing 'form_params' to 'query' gives me:

Client error: `POST https://exp.host/--/api/v2/push/send?to%5B0%5D=ExponentPushToken%5XXXXXXXXXX_Oi4J1b9OR%5D&title=asdasd&body=asdasdasd` resulted in a `400 Bad Request` response: {"errors":[{"code":"API_ERROR","message":"child \"to\" fails because [\"to\" is required], \"value\" must be an array."} (truncated...)
Joel
  • 3,166
  • 5
  • 24
  • 29

1 Answers1

1

Tackling your issues from the last one to the first:

1) The API endpoint accepts a POST request (Not a GET). These terms are not interchangeable. So the endpoint does not recognize your overloaded GET query string.

2) Mulipart form data needs to be formatted as Multipart form data. You have not done this. You have just switched the parameter to multipart from form_params.

3) And finally, the problem that started you off. Your "To" field should not be an array. I think you are getting a 404 error because there is no endpoint there that accepts the json message you are sending. Try creating the json message just like their hello world curl example and see if you can get a proper response from the endpoint.

curl -H "Content-Type: application/json" -X POST "https://exp.host/--/api/v2/push/send" -d '{ "to": "ExponentPushToken[xxxxxxxxxxxxxxxxxxxxxx]", "title":"hello", "body": "world" }'

Mark C.
  • 378
  • 1
  • 12