Closed (works as designed)
Project:
Services
Version:
6.x-3.3
Component:
Code
Priority:
Normal
Category:
Bug report
Assigned:
Unassigned
Reporter:
Created:
20 Mar 2013 at 13:35 UTC
Updated:
26 Mar 2013 at 17:51 UTC
Hi all,
I've set up a Services 6.x-3.3 access point at /api with all the 'user' type resources switched on. I've modified some CURL code I found on the web to submit a user login request through JSON
$request_url = 'http://<siteurl>/api/user/login';
$user_data = array(
'username' => 'username',
'password' => 'test'
);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $request_url);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($user_data));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_FRESH_CONNECT, 1);
$response = curl_exec($curl);
$http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
print_r($http_code);
// Check if login was successful
if ($http_code == 200) {
echo $response;
}
else {
// Get error msg
$http_message = curl_error($curl);
die($http_message);
}
The login part works fine. But the $response returned by the server is not properly formatted JSON - it's a string with session and user information concatenated without any separation. Something like "f33bf8d92ec87aabb4f825fdd845e280SESS707500f7eec141cfda8fbc15a7a683be1use......" (double quotes not included). As a result I can't decode it into something more meaningful.
Am I missing something here?
Comments
Comment #1
kylebrowning commentedIm not sure why youre getting that, but I cannot reproduce.
When I http POST d6.local.com/api/user/login username=admin password=admin
I get
Comment #2
kylebrowning commentedMaybe try setting the Accept header to be application/json
Content-Type only specifies what your sending.
Comment #3
jay_N commentedKyle, thanks! The Accept header was exactly what was missing. I now get a nicely formatted JSON response.
For anyone else's benefit, here's my working code for the 'user.login' method in JSON format:
Comment #4
kylebrowning commentedMight want to edit that to remove the credentials and real URL?
Comment #5
kylebrowning commentednevermind, you did.
CARRY ON!