11

I am using Drupal 7 and the Services module 7.x-3.2. I am using Postman for Chrome (a http client) to test the API.

What I want to do is log in a user and create a node with the user using the Drupal service API.

I logged in the user with JSON POST request to http://example.com/api/user/login.

enter image description here

I successfully got the JSON result.

{
    "sessid": "KrZjlcY3rFr8v6iLoskcVQN9DmDWsg5yZqIjBeydEBY",
    "session_name": "SESS9fe39db36ccd7afd64e034fefa0129e1",
    "user": {
        "uid": "31",
        "name": "test_user",
        "mail": "services_user_2@example.com",
        "theme": "",
        "signature": "",
        "signature_format": "filtered_html",
        "created": "1352985941",
        "access": "1352991170",
        "login": 1352991510,
        "status": "1",
        "timezone": "",
        "language": "",
        "picture": null,
        "init": "services_user_2@example.com",
        "data": false,
        "roles": {
            "2": "authenticated user"
        },
        "rdf_mapping": {
            "rdftype": [
                "sioc:UserAccount"
            ],
            "name": {
                "predicates": [
                    "foaf:name"
                ]
            },
            "homepage": {
                "predicates": [
                    "foaf:page"
                ],
                "type": "rel"
            }
        }
    }
}

Then I tried to create an article via the API by sending the JSON POST request. (The user has the permission to create Article nodes.)

enter image description here

I got a 401 Unauthorized response.

I guess I need to include the session cookie information to send the POST request, to create a node.

I have the session information previously returned.

"sessid": "KrZjlcY3rFr8v6iLoskcVQN9DmDWsg5yZqIjBeydEBY",
"session_name": "SESS9fe39db36ccd7afd64e034fefa0129e1",

How can I utilize it to make a successful JSON POST request for creating a node?

  • 1
    hi, I have the same problem. Did you got it worked? – Ramaraju.d Jun 25 '13 at 12:52
2
{
   "title": "Just another drupal Post",
   "type": "Article"
}

notice no comma after the type:article tag so that your json works.

2

Hope this will help you,

  • To make the HTTP requests, Im using the Firefox add-on POSTER.

    enter image description here

    {
      "type" : "article",
      "title" : "Test article with language",
      "language" : "und"
    }
  • If node is created successfully you will get a response with a link.

    enter image description here

  • Examples

    {
      "type": "article",
      "title": "Test article",
      "body": { 
        "und": [{ 
          "value": "test body!"
         }]
      },
      "language" : "und"
    }
    
    {
       "title":"Review",
       "type":"establishment_review",
       "field_establishment":[
          {
             "nid":{
                "nid":"[nid:26686]"
             }
          }
       ],
       "field_fivestar_value":[
          {
             "rating":"20",
             "target":"0"
          }
       ],
       "field_review":[
          {
             "value":"comments about the restaurant"
          }
       ]
    }
    

  • You can refer this

  • I have answered How do I POST a new node using the Services module question. You can refer for more information.

-1

You need to set cookie="sessionname=sessionid" value in headers, where sessionname comes from the endpoint/user/login URL.

  • You need to set cookie="sessionname=sessionid" value in headers in postman , where sessionname comes from the endpoint/user/login URL. – Gholave Ranjeett Vijaykumar Apr 23 '14 at 15:31

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.