I have built an API endpoint from an external server to prevent CORS issue when using the HubSpot Events HTTP API to make a request.
The Events API will return an image/gif as a response. Here's an example HS Events API request:
https://track.hubspot.com/v1/event?_n=000000001625&_a=62515&email=testingapis@hubspot.com
And here is my created API endpoint built within WP Rest API:
function hs_events_api_call( $data ) {
if ( empty( $data ) ) {
return null;
}
$token = "my-token";
$endpoint = 'https://track.hubspot.com/v1/event';
$hubId = "my-hubid";
$required['_a'] = $hubId;
$required['_n'] = $data["eventid"];
$required['email'] = $data->get_param( 'email' );
$query_string = build_query_string($required);
$args = array(
'method' => 'GET',
'headers' => array(
'Authorization' => 'Bearer ' . $token,
'Content-Type' => 'application/json'
),
);
$url = $endpoint.'?'.$query_string;
$request = wp_remote_get( esc_url_raw( $url ), $args )["body"];
if( empty( $request ) ) {
return null;
}
$response = new WP_REST_Response($request);
$response->set_status(200);
$response->header('Content-Type', 'image/gif');
return $response;
}
The problem with the endpoint I created is that it returns the image binary as a string. Specifically looking like this:
"GIF89a\u0001\u0000\u0001\u0000?\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000!?\u0004\u0001\u0000\u0000\u0000\u0000,\u0000\u0000\u0000\u0000\u0001\u0000\u0001\u0000@\b\u0004\u0000\u0001\u0004\u0004\u0000;"
How can I convert it so it outputs as an image file, specifically, an "image/gif" type?
Or is possible at all?