• Hello! Is there a way to display the webhook response? I’m looking to post an error message if the http status code is not 200.

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author Mário Valney

    (@mariovalney)

    Thread Starter huynhr36

    (@huynhr36)

    Thanks for the link Mario. Do you have any supporting documents on how I can display any API post response even if there’s no error?

    I found out the API connection I’m setting up only sends status code “201” back even if there’s issues with the form data so I’m trying to figure out a conditional statement that would pull the variable “outcome” from the response body.

    Here’s an example of the response code:

    {

    “outcome”: failure

    “Reason”: “phone number is not valid”

    }

    Plugin Author Mário Valney

    (@mariovalney)

    The filter ctz_post_request_result can be used for it too: all requests will be filtered.

    Thread Starter huynhr36

    (@huynhr36)

    Thanks for the info! I’m struggling on how to get the response body added into a variable to print the exception area. I understand what “$response[‘code’]” is, but how do I get the response body?

    Plugin Author Mário Valney

    (@mariovalney)

    The action will receive everything from wp_remote_post call and you can check it in details on wp_remote_request documentation.

    add_action( 'ctz_post_request_result', 'example_ctz_post_request_result' );
    function example_ctz_post_request_result( $result ) {
        $body = ( ! empty( $result['body'] ) ) ? $result['body'] : [];
    
        // You have a body now... Do what you want to do.
        
        // I didn't try this code.
        // But... you can do something like that:
        $submission = WPCF7_Submission::get_instance();
        $submission->set_status( 'mail_sent' );
        $submission->set_response( 'The message you want...' );
    }

    Change “mail_sent” for any status you want. A list from CF7 code:

    • validation_failed
    • acceptance_missing
    • spam
    • mail_sent
    • mail_failed

    Or just throw a exception if you want to add a custom error.

    Thread Starter huynhr36

    (@huynhr36)

    Thanks for sharing and being so responsive! That worked on my end!

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Webhook API Response’ is closed to new replies.