API Using wrong content type

By: Matt Saint | Asked: 08/23/2022
ForumsCategory: General questionsAPI Using wrong content type
Matt Saint asked 2 years ago

Hi All,  

 

I upgraded today to use the API plugin, so we can create a ticket in our Freshdesk ticketing platform. Looks to be a straight forward setup but I am receving an error in the log saying that the content type is wrong even though I have selected JSON.

Does anyone here have any ideas on why this would be the case? Formidbale Support are blaming the third party but I do not  think that is the case as I can create a ticket via Curl as long as I set the right content type. Setup screenshot attached.

Error Message: \"code\": \"invalid_content_type\", \"message\": \"Content-Type header is set to application/x-www-form-urlencoded. It should be set to application/json\"}\", \"response\":

Details
WP Version: 6.0.1
Formidable Version: 5.4.4

Formidable API 1.11

Regards

Matt

Attachments
2 Answers
Victor Font answered 2 years ago
When you're testing an API connection, thee are several steps that need to be tested.
  1. First, use Postman to test the API before attempting to setup Formidable. If it works with Postman, it will work with Formidable.
  2. Install the Formidable logs add-on so you can view the results of Formidable's attempt.
  3. Make certain data is sent in the exact format required by the API. The most common mistake is not sending the correct date formats.
Matt Saint replied 2 years ago

Thanks Victor.

The error I posted is from the logs and the API works via curl from the CLI. Just need a way to force formidable to send the right content type.

Matt

Victor Font replied 2 years ago

Send it as raw JSON.

Matt Saint answered 2 years ago

Hi,
All sorted. The issue is only related to the test connection button. That ignores the field settings. 
 
The ajax function lnked to the button is very basic. I added a content type and data type to the ajax and changed the returnerror.

function frmapi_test_connection(){
        var settings = jQuery(this).closest('.frm_single_api_settings');
        var key = settings.data('actionkey');
        var baseName = 'frm_api_action['+key+'][post_content]';
 
        var url = jQuery('input[name="'+baseName+'[url]"]').val();
        var key = jQuery('input[name="'+baseName+'[api_key]"]').val();
        var testResponse = settings.find('.frmapi_test_resp'),
                button = this;
 
        if (url == '') {
                settings.find('.frmapi_test_connection').html('Please enter a URL');
                return;
        } else if ( url.indexOf('[') != '-1' ) {
                testResponse.html('Sorry, Dynamic URLs cannot be tested');
                return;
        }
 
        button.classList.add( 'frm_loading_button' );
        testResponse.html('');
 
        jQuery.ajax({
                type:'POST',url:ajaxurl,
                contentType: "application/json",
                dataType: "json",
                data:{action:'frmapi_test_connection',url:url,key:key},
                success:function(html){
                        testResponse.html(html);
                        button.classList.remove( 'frm_loading_button' );
                }
        });
}
 
 

Regards
Matt

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