Switch between different content types and test, for example, application/json works perfectly for me. You need to do the following steps:
Request CSRF token /services/session/token (sample request below)
request.get({url:'https://serveraddress/services/session/token'}, function(error, response, body) {
if(!error && response.statusCode == 200) {
console.log('CSRF Token obtained : ' + body);
} else {
console.log('Failed to retrieve CSRF token.');
}
Make use of the obtained CSRF token to perform authentication (if needed), sample below
// Set the headers
var headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'X-CSRF-Token' : token
};
// Configure the request
var options = {
url: 'https://serveraddress/servicesendpoint/user/login.json',
method: 'POST',
headers: headers,
body: 'username=yourusername&password=yourpassword'
};``
// Start the request
request(options, function (error, response, body) {
if (!error && response.statusCode == 200) {
var jsonBody = JSON.parse(body);
console.log('Authentication succeeded, cookie value is : ' + body);
console.log('SESSID : ' + jsonBody.sessid);
console.log('SESSION_NAME : ' + jsonBody.session_name);
console.log('Token : ' + jsonBody.token);
} else {
console.log('Authentication failed. Error is : ' + error);
console.log('Authentication failed. Body is : ' + body);
console.log('Authentication failed. Response is : ' + JSON.stringify(response));
}
});
Now that you have SESSION NAME & SESSION ID, you can proceed with updating a node. See sample below.
//set headers
var headers = {
'X-CSRF-Token' : authResult.token,
'Content-Type': 'application/json',
'Cookie' : authResult.session_name + '=' + authResult.sessid
};
// Configure the request
var options = {
url: 'https://serveraddress/serviceendpoint/node/' + user.result.nid,
method: 'PUT',
headers: headers,
json: data
};
request(options, function (error, response, body) {
if(!error) {
console.log('Drupal node is updated successfully!');
console.log(body);
} else {
console.log('Updating Drupal node failed! Error status : ' + response.statusCode);
console.log('Updating Drupal node failed! Error body : ' + JSON.stringify(body));
}
});
If you notice above, I am console logging all the errors that is returned from the Services, so that I have something to Google and figure out.
For those who wish to know how to upload a file and associate them to a node. Assuming that Service Endpoint is configured to accept file uploads. You will need a sample request as below:
//data
var data = {
"uid":"1",
"filesize":fileSizeInBytes,
"filename":user.firstName + '_' + user.lastName,
"file": base64Image
};
var options = {
url: 'https://serveraddress/servicesendpoint/file',
method: 'POST',
headers: headers,
json: data
};
request(options, function(error, response, body) {
if(!error) {
//photo is uploaded, returning fid
console.log('Publishing Image to Drpual succeeded. FID is : ' + body.fid);
} else {
console.log('Publishing Image to Drpual failed. Response is : ' + JSON.stringify(response));
console.log('Publishing Image to Drpual failed. Body is : ' + JSON.stringify(body));
}
});
Attach the returned FID to the request that is supposed to update the node.
Sample JSON to update a field of a content type would look like this:
data = {
"body": {
"und": [
{
"value": user.profile,
"format": "full_html"
}
]
},
"field_member_position": {
"und": [
{
"value": user.title
}
]
},
"field_member_photo" : {
"und" : [
{
"fid" : fid
}
]
}
};
}
Hope above help those struggling with using Services 3. The code is written in Express JS 4 and I have used the request NPM module.
A simple checklist for Services 3 would be:
- Configure the module (request content type, as well as allow CRUD on nodes/files as per your need)
- Make sure you are hitting the correct URL to get token, authenticate and update node, upload file
Watch for famous errors such as Not Acceptable Content type, Trying to Authenticate as anonymous user, A particular field is required (username/password), others
- When trying to update a custom field, it is more useful to SEE that particular node's structure via DEVEL and then accordingly structure your JSON data.
application/json
with POST/PUT when creating/updating users with fields.