• Resolved drea21

    (@drea21)


    Hello,

    I’m trying to create a custom action that will receive data from TypeForm that I can then use to create a new user and update user meta. When I send a request from Typeform, I get a response, but I am not able to see the data in the Request payload in my action function.

    Here is my code:
    add_filter( 'wpwhpro/run/actions/custom_action/return_args', array($this, 'my_custom_callback_function' ) );

    public function my_custom_callback_function( $return_args = null, $identifier = null, $response_body = null ){
    		var_dump($_POST);
    		var_dump($response_body['content']);
    
    		return $return_args;
    	}

    How can I see the data from the request? The var_dumps are empty array and NULL.

Viewing 9 replies - 1 through 9 (of 9 total)
  • Plugin Contributor Ironikus

    (@ironikus)

    Hi @drea21 – thank you a lot for your message, as well as for using our plugin.
    Have you seen the example we provided within the webhook description?
    This one contains all the code you need.
    With the code you provided, you get back NULL since the request that Typeform sent might not be saved within the $_POST variable since it might uses a different content type.
    From your code, I also can see that the $response_body variable is nowhere defined. That’s why you don’t see any data.

    Here is again the full example for reference, which contains everything you need:

    add_filter( 'wpwhpro/run/actions/custom_action/return_args', 'wpwh_fire_my_custom_logic', 10, 3 );
    function wpwh_fire_my_custom_logic( $return_args, $identifier, $response_body ){
    
    	//If the identifier doesn't match, do nothing
    	if( $identifier !== 'ilovewebhooks' ){
    		return $return_args;
    	}
    
    	//This is how you can validate the incoming value. This field will return the value for the key user_email
    	$email = WPWHPRO()->helpers->validate_request_value( $response_body['content'], 'user_email' );
    
    	//Include your own logic here....
    
    	//This is what the webhook returns back to the caller of this action (response)
    	//By default, we return an array with success => true and msg -> Some Text
    	return $return_args;
    
    }

    In case you are not sure enough on how to use WordPress hooks/filters, I suggest contacting a developer for help since you can easily break your site this way.
    I hope this helps so far.
    If you have further questions, feel free to reach out at any time. ??

    Thread Starter drea21

    (@drea21)

    Hi Ironikus,

    The response_body variable is the third argument of the callback just like the example. Nothing gets passed in it and if I don’t set it as a default to null, it throws an error.

    I can from Typeform tests it is sending a payload, but the webhook does not seem like it is reading the request body payload and passing it to my filter. Is there another way to access the data sent to a custom action webhook?

    Plugin Contributor Ironikus

    (@ironikus)

    Hello @drea21 – thank you a lot for your message and sorry for the inconvenience with the variable.

    Are you able to share some further details on the webhook request from Typeform?
    If there is any documentation available or more details about the payload/request, I’ll check against that and advise you what to do.
    Thank you already a lot.

    Thread Starter drea21

    (@drea21)

    Yeah I can share more details. You can try making a Typeform for yourself and adding a webhook to a WP Webhooks endpoint to see if you can get the submissions to come through. I’ll give you more details about the request.

    The Content-Type is application/json.

    The body looks like:

    {
      "event_id": "ABCD12345",
      "event_type": "form_response",
      "form_response": {
        "form_id": "1234",
        "token": "SOME_TOKEN",
        "landed_at": "2020-06-07T19:56:03Z",
        "submitted_at": "2020-06-07T19:56:03Z",
        "definition": {
          "id": "ABCD",
          "title": "Question Title",
          "fields": [...]
        }
      }
    }

    I have obfuscated the ID and Token values and abbreviated the fields, but you get the idea.

    If you are looking for any other specific details, please let me know.

    Thanks!

    Plugin Contributor Ironikus

    (@ironikus)

    Hey @drea21 – thanks a lot for the additional details.
    I think I understand the issue and I believe it’s quite simple to fix.

    I assume the problem is that the endpoint does not know to which webhook action es is actually sending the data since there is no action key within the payload.
    Since you can’t edit the data, our plugin allows you to add the action within the URL of the webhook action URL. Simply add there something like &action=custom_action

    Hope this helps.
    Fee free to let me know of that’s works for you.

    Thread Starter drea21

    (@drea21)

    Hey @ironikus,

    Thanks for getting back to me. That is a good guess, but its not that. I saw that in the documentation and added it to the query string. I know the right function is getting invoked because its responding to Typeform and I can see the response as well as the var_dumps. The only problem is it seems like the response_body is null and there is no way to read the request body that I can see.

    This is the response I see:

    {"success":true,"msg":"Custom action was successfully fired."}

    • This reply was modified 4 years, 9 months ago by drea21.
    Plugin Contributor Ironikus

    (@ironikus)

    HEy @drea21 – you are totally right with that.
    Since there’s no values sent, I suggest you try to fetch the content directly from the payload body. You can do this using the following piece of code:
    $response = file_get_contents('php://input');

    If there is also nothing included, you might need to check if there is no other logic in place that clears these kind of data for security reasons.

    Thread Starter drea21

    (@drea21)

    @ironikus Thank you for getting back to me. I literally thought about using file_get_contents and decided not to try it because I thought there would be a cleaner way. Regardless, your solution worked. It looks like I can parse the body from php://input.

    Resolved!

    Plugin Contributor Ironikus

    (@ironikus)

    Hey @drea21 – thank you for the answer. That’s super strange since we use the same code within the get_response_body() function. The only additional thing we are doing is to validate the data based on the given content type.
    Therefore, since the data is there and I assume it is a JSON you grab from the payload, my guess is that the Content type that is set from the API is not application/json, but something else.
    I remember from past issues with other third-party services that the formatting was wrong since they sent a JSON for example as www-form-urlencode instead of with application/json.

    Hope this helps and if you have further questions, feel free to let me know.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Not able to use data from request in custom action’ is closed to new replies.