Send API Data action error handling

By: Gavin Harper | Asked: 11/04/2022
ForumsCategory: General questionsSend API Data action error handling
Gavin Harper asked 2 years ago
We extensively use the Send API Data action to submit form responses via API to our platform. We have the logging add-on enabled, which is great at logging what happened but you have to check the logs if you know there was an issue. We have had situations where a form was submitted and our API call failed, but we didn't know. Is there anyway of setting up an alert via email if the API response was anything other than a 200 OK message? Or, if the response was anything other than a 200, could we display a failure message to the end user so they know it was unsuccessful? Thanks   Gavin  
Question Tags:
3 Answers
Steven Sabold answered 2 years ago
It's amazing that this was left unanswered Gavin. You would think a notification styled error handling would be a default action for API calls. I did email the Formidable team about this question and they supplied a partial answer. They referenced this hook: https://formidableforms.com/knowledgebase/frm_api_post_response/ I attempted to utilize it to fire a wp_mail() function on the else condition, but the hook does not appear to fire when in my functions.php. I'm wondering if you had been able to solve your original inquiry from 5 months ago? Thanks, Steve
Victor Font replied 2 years ago

It would be helpful to know how you determined add_action("frmapi_post_response", "your-function", 10, 3) is not firing after you send an API request. Are you adding a break point to examine the values of the parameters $response, $entry, and $form_action? Please share how you performed the diagnostics?

Gavin Harper answered 2 years ago
Hey Steven. I found the same KB article and managed to use the examples to update a field in the record with the API status response.
add_action( 'frmapi_post_response', 'frm_save_api_response', 10, 3 );
function frm_save_api_response( $response, $entry, $form_action ) {
  $status = wp_remote_retrieve_response_code( $response );
    FrmProEntryMeta::update_single_field( array(
      'entry_id' => $entry->id,
      'field_id' => 11,
      'value'    => $status,
    ) );
}
Then, when field_id is anything other then a '200' I give the user an error and also send a Slack notification to our support team to investigate further. This has been working well so far. Regards, Gavin
Gavin Harper answered 2 years ago
Hey Steven. I found the same KB article and managed to use the examples to update a field in the record with the API status response.
add_action( 'frmapi_post_response', 'frm_save_api_response', 10, 3 );
function frm_save_api_response( $response, $entry, $form_action ) {
  $status = wp_remote_retrieve_response_code( $response );
    FrmProEntryMeta::update_single_field( array(
      'entry_id' => $entry->id,
      'field_id' => 11,
      'value'    => $status,
    ) );
}
Then, when field_id is anything other then a '200' I give the user an error and also send a Slack notification to our support team to investigate further. This has been working well so far. Regards, Gavin

Making the Best WordPress Plugin even better - Together

Take on bigger projects with confidence knowing you have access to an entire community of Formidable Experts and Professionals who have your back when the going gets tough. You got this!
Join the community
crossarrow-right