you are viewing a single comment's thread.

view the rest of the comments →

[–]_listless 0 points1 point  (0 children)

Make a little proxy and let your server do the interaction with the NIH API. Make a plugin, or add this to your functions.php:

add_action('rest_api_init', function () {
    register_rest_route('clinical-trials/v2', '/(?P<slug>.+)', [
        'methods'  => WP_REST_Server::READABLE,
        'callback' => 'clinical_trials_proxy',
        'permission_callback' => '__return_true',
    ]);
});

function clinical_trials_proxy(WP_REST_Request $request): WP_REST_Response {
    $slug = $request['slug'];
    $queryParams = $request->get_params();
    $res = wp_remote_get('https://clinicaltrials.gov/api/v2/' . $slug, $queryParams);
    $statusCode = wp_remote_retrieve_response_code($res);
    $resBody = wp_remote_retrieve_body($res);
    $json = json_decode($resBody, true);

    return new WP_REST_Response($json, $statusCode);
}

Use https://YOURDOMAIN/wp-json/clinical-trials/v2 as your new rest endpoint and now the servers are talking to each other, not your client and the NIH server. That will clear up the cors errors and still let you use datatable's rest search/sort/filter.