5

I work with Drupal 7 (services 3.x) to implement a REST service. When I test a login request with this script:

$service_url = 'https://dev.mysite.org/api/blog/user/login.xml'; // .xml asks for xml data in response
$post_data = array(
  'username' => 'admin',
    'password' => 'admin',
    );
$post_data = http_build_query($post_data, '', '&'); // Format post data as application/x-www-form-urlencoded
// set up the request
$curl = curl_init($service_url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);  // have curl_exec return a string
curl_setopt($curl, CURLOPT_POST, true);             // do a POST
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data); // POST this data
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
// make the request
curl_setopt($curl, CURLOPT_VERBOSE, true); // output to command line
$response = curl_exec($curl);
curl_close($curl);
print "RESPONSE:\n";
var_dump($response);
// parse the response
$xml = new SimpleXMLElement($response);
$session_cookie = $xml->session_name .'='. $xml->sessid;
print "SESSION_COOKIE: $session_cookie";
file_put_contents('session_cookie.txt', $session_cookie);

I receive this error:

* upload completely sent off: 29 out of 29 bytes
< HTTP/1.1 401 Unauthorized: Missing required argument name
< Date: Sun, 09 Feb 2014 02:39:45 GMT
* Server Apache/2.2.22 (Ubuntu) is not blacklisted
< Server: Apache/2.2.22 (Ubuntu)
< X-Powered-By: PHP/5.3.10-1ubuntu3.9
< Expires: Sun, 19 Nov 1978 05:00:00 GMT
< Last-Modified: Sun, 09 Feb 2014 02:39:45 +0000
< Cache-Control: no-cache, must-revalidate, post-check=0, pre-check=0
< ETag: "1391913585"
< Set-Cookie: DRUPAL_UID=-1; expires=Tue, 04-Mar-2014 06:13:05 GMT; path=/; domain=.dev.labdoo.org; secure
< Vary: Accept-Encoding
< Content-Length: 0
< Content-Type: text/html
< 
* Connection #0 to host dev.mysite.org left intact
RESPONSE:
string(0) ""
PHP Fatal error:  Uncaught exception 'Exception' with message 'String could not be parsed as XML' in /home/myuser/Desktop/test.php:23
Stack trace:
#0 /home/myuser/Desktop/test.php(23): SimpleXMLElement->__construct('')
#1 {main}
  • My REST service configuration is:

enter image description here

enter image description here

enter image description here

  • I followed this example to test the service.
  • It is activated the session authentication in the Services module.
  • It is activated application/x-www-form-urlencoded, json, xml....

UPDATE

It seems which the code has multiple mistakes.... I replaced my post_data variable for this:

$post_data = array(
  'name' => 'admin',
  'pass' => 'admin',
);

And now, I send the login request correctly ( I receive a 200 response), but now, I try to parse the response message to get the cookie-session, I don't receive any response message. I am getting an error like this:

* upload completely sent off: 21 out of 21 bytes
< HTTP/1.1 200 OK
< Date: Wed, 12 Feb 2014 21:35:36 GMT
* Server Apache/2.2.22 (Ubuntu) is not blacklisted
< Server: Apache/2.2.22 (Ubuntu)
< X-Powered-By: PHP/5.3.10-1ubuntu3.9
< Expires: Sun, 19 Nov 1978 05:00:00 GMT
< Last-Modified: Wed, 12 Feb 2014 21:35:36 +0000
< Cache-Control: no-cache, must-revalidate, post-check=0, pre-check=0
< ETag: "1392240936"
< Set-Cookie: DRUPAL_UID=-1; expires=Sat, 08-Mar-2014 01:08:56 GMT; path=/; domain=.dev.mysite.org; secure
< Content-Length: 1316
< Content-Type: application/xml
< 
* Connection #0 to host dev.mysite.org left intact
RESPONSE:
string(1316) "
Fatal error: Unsupported operand types in /var/www/lbd/profiles/labdoo/modules/contrib/services/includes/services.runtime.inc on line 417

Call Stack:
    0.0000     334912   1. {main}() /var/www/lbd/index.php:0
    0.0347    4175712   2. menu_execute_active_handler() /var/www/lbd/index.php:21
    0.0348    4176376   3. call_user_func_array() /var/www/lbd/includes/menu.inc:517
    0.0348    4176756   4. services_endpoint_callback() /var/www/lbd/includes/menu.inc:517
    0.0366    4343036   5. call_user_func() /var/www/lbd/profiles/labdoo/modules/contrib/services/services.module:218
    0.0366    4343036   6. rest_server_server() /var/www/lbd/profiles/labdoo/modules/contrib/services/services.module:218
    0.0385    4449312   7. RESTServer->handle() /var/www/lbd/profiles/labdoo/modules/contrib/services/servers/rest_server/rest_server.module:39
    0.0432    4532668   8. services_controller_execute() /var/www/lbd/profiles/labdoo/modules/contrib/services/servers/rest_server/includes/RESTServer.inc:46
    0.0436    4534132   9. _services_controller_execute_internals() /var/www/lbd/profiles/labdoo/modules/contrib/services/includes/services.runtime.inc:106
    0.0436    4534176  10. _services_authenticate_user() /var/www/lbd/profiles/labdoo/modules/contrib/services/includes/services.runtime.inc:162

UPDATE 2

I am trying to create a new user to check if my error is related to working with an admin account. I tried (now with the script postman for Chrome) to create a new user via REST... It would be easy... but I wasn't ( I tested username,name,email,mail)...enter image description here

2

This is the important bit:

HTTP/1.1 401 Unauthorized: Missing required argument name

Just change your payload to

$post_data = array(
  'name' => 'admin',
  'password' => 'admin',
);
  • Sorry @Clive, Is no it correct yet? I added the payload as you comment. – carlos.baez Feb 12 '14 at 0:19
2

Try watching this screencast, it helped me

  • Thanks @sibiru. For the first tutorial, I followed all these steps, It helped me to fix my bug.... and with the second tutorial, I would not implement tokens in my service yet.... I would like implement a simple authenticated service, tokens could be too complicated. – carlos.baez Feb 12 '14 at 21:43
0

Finally, I have fixed the problem. It was a bug in the Services module. I updated from 7.x-3.5 to the 7.x-3.7 version, I followed the @sibiru tutorial and it started to work well.

  • Hi i am already using 7.x-3.15 but i am still getting this error – Naveen Aug 17 '16 at 10:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.