1

I'm using Postman in Chrome to test my services. I'm sucessfully able to create user accounts using the following:

Headers:

  • Content-Type /application/x-www-form-urlencoded
  • X-CSRF-Token 84t_NqiZcxiK42Obcy4aIpT7oxhar4DJvf7hIvRSxJE

x-www-form-urlencoded data

  • Name = testname1
  • mail = testmail1@localhost.com
  • pass = pass

This works, but to update a custom field, nothing seems to work. My field is a text field with the machine name field_test_text, I've tried adding the value

  • field_test_text[und][0][value] = TEST

But this doesn't work, however the user is still created.

  • For some reason, I think you can only use Content-Type application/json with POST/PUT when creating/updating users with fields. – tyler.frankenstein Apr 23 '15 at 0:55
2

Switch between different content types and test, for example, application/json works perfectly for me. You need to do the following steps:

  1. 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.');
    } 
  2. 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));
        }
    });
    
  3. 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:

  1. Configure the module (request content type, as well as allow CRUD on nodes/files as per your need)
  2. Make sure you are hitting the correct URL to get token, authenticate and update node, upload file
  3. Watch for famous errors such as Not Acceptable Content type, Trying to Authenticate as anonymous user, A particular field is required (username/password), others

    1. 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.

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.