Validation error even when parameter passed while running data explorer API with Curl

I have looked at forum answers but I still can’t make it to work. The function does not want to take a user_param parameter I have defined in the query.

Error: Array ( [success] => [errors] => Array ( [0] => DataExplorer::ValidationError: Missing parameter user_param of type int ) )

The query:

-- [params]
-- int :user_param

SELECT Count(likes_received) total_likes_received 
from user_stats us
where us.user_id = :user_param

The code:

<?php global $post;

$current_user_id = get_current_user_id();

$user_discourse_id = (int)get_user_meta($current_user_id,'discourse_sso_user_id')[0];
//api credentials,
$api_key = 'key';
$api_username = 'name';
$post_fields = array(
    'user_param' => $user_discourse_id,
);

$headers = array("Content-Type: multipart/form-data;","Api-Key: $api_key","Api-Username: $api_username",);
$url = 'https://domain/admin/plugins/explorer/queries/9/run';
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $post_fields );

$result = curl_exec( $ch );
if ( curl_errno( $ch ) !== 0 ) {
    // Handle error, call curl_close( $ch ) and return.
	echo 'error';
	curl_close ($ch);
	return;
}

curl_close( $ch );
$decoded_result = json_decode( $result, true );
print_r($decoded_result);

?>

I have generated value for $post_fields with and without http_build_query but still parameter is not passed to data explorer.

This was working a month ago with http_build_query and now it is not. What am I missing here?

Update:
I even tried to pass the array manually and still the same error. Has something recently changed in the plugin?

curl_setopt( $ch, CURLOPT_POSTFIELDS, array('user_param'=>'25'));
curl_setopt( $ch, CURLOPT_POSTFIELDS, array('user_param'=> 25));

Have a look at How to run Data Explorer queries with the Discourse API for details about how the params parameter needs to be set. Based off the code you have provided, it looks like you are trying to pass the user_param param directly instead of including it in a params param. The first code example in the topic that I linked to should give you an idea about how to set that parameter.

Simon,

I looked at your article on APIs but I am using curl_setopt() function to insert the parameters. The function requires me to use an array. so I have tried the following and it is displaying a forum level error.

$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch, CURLOPT_POSTFIELDS, array('params' => (array('user_param' => 25))));

$result = curl_exec( $ch );
print_r($result);

Seems like the data is passing now but the output is not being rendered.

Oops

The software powering this discussion forum encountered an unexpected problem. We apologize for the inconvenience.

Detailed information about the error was logged, and an automatic notification generated. We’ll take a look at it.

No further action is necessary. However, if the error condition persists, you can provide additional detail, including steps to reproduce the error, by posting a discussion topic in the site’s feedback category.

What am I missing?

You might be able to simplify debugging the issue by first attempting to make the API call with a curl request from your computer’s terminal. If that can be made to work, you’ll know that the issue is related to how you are creating the request with curl_setopt.

I wouldn’t expect making a Data Explorer query via the API to cause a 500 error on your site’s server. If you are able to access the site, you should find details about what has gone wrong in your site’s error logs (found at Admin / Logs / Error Logs.)

I was able to find the information but cannot make sense of it.

MultiJson::ParseError (unexpected character (after ) at line 1, column 1 [parse.c:769])
app/controllers/application_controller.rb:395:in `block in with_resolved_locale'
app/controllers/application_controller.rb:395:in `with_resolved_locale'
lib/middleware/omniauth_bypass_middleware.rb:71:in `call'
lib/content_security_policy/middleware.rb:12:in `call'
lib/middleware/anonymous_cache.rb:355:in `call'
config/initializers/008-rack-cors.rb:25:in `call'
config/initializers/100-quiet_logger.rb:23:in `call'
config/initializers/100-silence_logger.rb:31:in `call'
lib/middleware/enforce_hostname.rb:23:in `call'
lib/middleware/request_tracker.rb:177:in `call'

Regarding your first suggestion, I am working on figuring out how to run Curl on command prompt.

1 Like

So I tried the code on terminal and I am getting the following error:

curl: (3) URL using bad/illegal format or missing URL
curl: (3) URL using bad/illegal format or missing URL
curl: (3) URL using bad/illegal format or missing URL
curl: (3) URL using bad/illegal format or missing URL

root@Discourse-for-Prowess:~# curl -X POST “https://domain/admin/plugins/explorer/queries/9/run”-H “Content-Type:multipart/form-data;”-H “Api-key:12345”-H “Api-username:username”-F ‘params={“user_param”:“25”}’

I am able to run the query in data explorer - screenshot.

In the error logs I see the following:

ActionDispatch::Http::MimeNegotiation::InvalidType ("%{#context['com.opensymphony.xwork2.dispatcher.httpservletresponse'].addheader('90b3sfq9'" is not a valid MIME type)
lib/middleware/omniauth_bypass_middleware.rb:71:in `call'
lib/content_security_policy/middleware.rb:12:in `call'
lib/middleware/anonymous_cache.rb:353:in `call'
config/initializers/008-rack-cors.rb:25:in `call'
config/initializers/100-quiet_logger.rb:23:in `call'
config/initializers/100-silence_logger.rb:31:in `call'
lib/middleware/enforce_hostname.rb:23:in `call'
lib/middleware/request_tracker.rb:177:in `call'

Any suggestion would be helpful.