Internal server error 500 when submitting form for POST request Jira API

582 Views Asked by At

Good day everyone, I have some questions and errors about using the Jira API for my WordPress website and I need your help on this

So I am building a WPForm in WordPress to take the data from the inputs and submit them with Jira API POST request to create a task on Jira. However, I encountered some errors when submitting the form. Below is the code

function sendingDataToJira( $fields, $entry, $form_data, $entry_id) {

  $user = 'MY_USERNAME';
  $token = 'MY_TOKEN';

  $entry = wpforms()->entry->get( $entry_id );
  $entry_fields = json_decode( $entry->fields, true);

  if ($form_data['id'] == 3809) {
    $api_url = 'https://MY_URL/rest/api/2/issue/';
    $body = array (
      'fields' => array (
        'project'=> array (
          'key'=> 'ATS',
         ),
        'summary' => 'New Applicant',
        'description' => 'null',
        'customfield_10034' => $form_data[4]['value'],
        'customfield_10035' => $form_data[5]['value'],
        'issuetype' => array(
          'name' => 'Task',
        ),
     ),
   );

  $request = wp_remote_post( $api_url, array(
    'method' => 'POST',
    'headers' => array(
      'Authorization' => 'Basic' . base64_encode( $user . ':' . $token),
      'Content-Type' => 'application/json'),
    'body' => json_encode( $body ),
    'data_format' => 'body'
) );

  };
}

add_action( 'wpforms_process_complete', 'sendingDataToJira', 10, 4 );

I tried using other third party plugins but they might require premium version as well as my boss told me not to use any third parties. I also tried switching to Contact Form 7 with API integration but it didn't work as well.

I want it to work like after pressing the submit button, the form will send a POST request to Jira and create a task.

Here are the errors that I got from the console: errors

POST https://olinno.com/wp-admin/admin-ajax.php 500

WPForms AJAX submit error

Here is the debug.log file after the errors

    [08-Dec-2022 07:37:38 UTC] PHP Fatal error:  Uncaught Error: Call to a member function get() on null in /home/u435205697/domains/olinno.com/public_html/wp-content/themes/landio/functions.php:127
    Stack trace:
    #0 /home/u435205697/domains/olinno.com/public_html/wp-includes/class-wp-hook.php(307): sendingDataToJira(Array, Array, Array, 0)
    #1 /home/u435205697/domains/olinno.com/public_html/wp-includes/class-wp-hook.php(331): WP_Hook->apply_filters(NULL, Array)
    #2 /home/u435205697/domains/olinno.com/public_html/wp-includes/plugin.php(476): WP_Hook->do_action(Array)
    #3 /home/u435205697/domains/olinno.com/public_html/wp-content/plugins/wpforms-lite/includes/class-process.php(450): do_action('wpforms_process...', Array, Array, Array, 0)
    #4 /home/u435205697/domains/olinno.com/public_html/wp-content/plugins/wpforms-lite/includes/class-process.php(109): WPForms_Process->process(Array)
    #5 /home/u435205697/domains/olinno.com/public_html/wp-content/plugins/wpforms-lite/includes/class-process.php(875): WPForms_Process->listen()
    #6 /home/u435205697/domains/olinno.com/pu in /home/u435205697/domains/olinno.com/public_html/wp-content/themes/landio/functions.php on line 127
1

There are 1 best solutions below

0
On

Most likely this add_action( ... ); is called too early, before WPForms plugin was able to load itself (but after WordPress, given that there is some output in the debug.log file).

Create a /mu-plugins/custom-wpforms.php file, and put your code over there, or create a custom plugin for that purpose, or use a plugin like WPCode for this.

Also, why do you re-declare the $entry variable? You receive it in function parameters from the hook, so you already have all the entry information - technically you don't need to make an additional request for that. $entry_fields is also an unused variable.

You can also use

add_action( 'wpforms_process_complete_3809', 'sendingDataToJira', 10, 4 );

instead and remove the if form ID check in your function completely.