General developer forum

 
 
Picture of Maciej Lisiewski
Web services input validation problem
 

I'm adding grade sync feature to a 3rd party product using web service added with a plugin 

The data I'm sending to Moodle is simple:

array(

    'courseid' => 10,
    'grades' => array(
        array(
            'grade' => 50.0,
            'userid' => 20
        ),         array(             'grade' => 55.5,
            'userid' => 21
        )
    )
)

It would seem that a _parameters() function in the web service would be simple:

return new external_function_parameters(
    array(
        'courseid' => new external_value(PARAM_INT, 'course id'),
        'grades' => new external_multiple_structure(
            new external_single_structure(
                array(
                    'userid' => new external_value(PARAM_INT, 'user id'),
                    'grade' => new external_value(PARAM_FLOAT, 'grade')
                )
            )
        )
    )
);

An yet I end up with "grades => Invalid parameter value detected: Only arrays accepted. The bad value is: 'userid'" no matter what I do. I've spent 2 days trying different code in _parameters function without any success. I have made sure that the data submitted is correct.

Please help.

 
Average of ratings: -
Picture of Maciej Lisiewski
Re: Web services input validation problem
 

Bump. I could really use some help with this

 
Average of ratings: -
Anne Krijger
Re: Web services input validation problem
Group Particularly helpful Moodlers

Hi Maciej,

It's always useful to know what version of Moodle we're talking about.

My suggestion would be to find out where in the code this exception is thrown (that should be easy to find) and possibly use a debugger to see what exactly is going wrong.

I only found 4 places in Moodle 2.4.x where this can occur.

If you feel real adventurous you could write a Unit test for the webservice and use that while debugging.

Anne.

 
Average of ratings: -
Picture of Maciej Lisiewski
Re: Web services input validation problem
 

It's Moodle 2.4.

I've made some progress with the problem - it actually required one more level of arrays, like this:

array(

    'courseid' => 10,
    'grades' => array(
        array(
            array(
                'grade' => 50.0,
                'userid' => 20
            ),
        ),
         array(
            array(
                'grade' => 50.0,
                'userid' => 19
            ),
        ),
    )
)

Don't ask my why, because I don't know - it's just a random thing I tried out of desperation and it got me  a bit further.

Now I'm stuck with:

grades => Invalid parameter value detected: Only arrays accepted. The bad value is: '{'grade': 50, 'userid': 20}'.
I know it looks like json, but I'm not POSTin json, but a Python dictionary (rough equivalent of PHP associative array). 
Is there a way I could json_decode before validation? I could just send json and skip the whole format issue..

 
Average of ratings: -
Picture of Maciej Lisiewski
Re: Web services input validation problem
 

I've ended up POSTing the data as json, "validating" it as PARAM_RAW and then doing real validation in the function that actually processes the request.

 
Average of ratings: -
Picture of einat dagan
Re: Web services input validation problem
 

try to work with only one array under grades array. that means remove one of the arrays:

array(

    'courseid' => 10,
    'grades' => array(
        array(
            'grade' => 50.0,
            'userid' => 20
            )
        )
    )
)

I undrestand thats not what you wanted but first try only to test if it works for you.

I experienced a similar problem in other web services calls.

Einat.

 
Average of ratings: -
Anne Krijger
Re: Web services input validation problem
Group Particularly helpful Moodlers

Could you post the complete url that you are using to the data mentioned above?

Anne.

 
Average of ratings: -
Picture of Maciej Lisiewski
Re: Web services input validation problem
 

I'm POSTing it to /webservice/rest/server.php and it goes to a web services function I'm trying to add.

 
Average of ratings: -