1

I am writing a Ruby script to ingest content into Drupal 7. This is on my local machine. I'm using the Services module and its rest service to GET and POST. I could log in and grab the csrf token and the cookie information but I get a 403 error when I try to post a new node. I checked my local apache access and error logs. There were no errors listed and the access log only reports the status. This is my code so far:

require "rest-client"
require "json"

node = create_node(node_hsh)
node_json = JSON.generate(node)
response = RestClient.post('localhost/drupal/restfulapi/node', node_json, 
:content_type => :json, 
:accept => :json, 
:headers => {:cookie => cookie, 'x-csrf-token' => csrf_token} 
)

I'm not sure where else to look? I would really appreciate any pointers. Thanks.

closed as too broad by kiamlaluno Apr 19 '18 at 16:13

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

1

I figured it out. Posting this for documentation: I had to pass the cookie after the user had logged in to do a GET on the token (localhost/services/session/token) for the Services module. Once I had the token, I could pass the cookie and the token to do a POST of the resources. I also switched the gem from rest-client to Faraday because I could customize my headers better. Also, the logging functionality was more verbose in Faraday which was helpful in debugging.

0

There are a couple ways to go about POSTing data to a resource. First, there is a handy Firefox plugin called Poster which allows the posting of arbitrary data to specified URLs. Poster can be downloaded from here.

For Google Chrome, you can use the Advanced REST Client extension found here.

It is fairly simple to use and it remembers your last entry so you can easily continue to test even after closing the browser.

The steps below are for Poster plugin for Firefox, but they are similar to Advanced REST Client for Chrome.

Here is a short tutorial on using Poster to POST data to create a new user through the user resource.

  1. After installing Poster you will need to reboot Firefox. Once you have rebooted you can active Poster from the menu at Tools->Poster.

  2. In the URL field, enter the base URL for your resource.

  3. Click the Parameters tab. This is where you enter the data to be submitted, which mimics the fields that would be posted from a web form.

Note that for other types of data like nodes, you will need quite a bit more information than this. For more information on what type of data is needed to create a resource, please reference the documentation for that resource's create method.

  1. Now click back to the Content to Send tab and click Parameter Body. This will convert your Parameters to properly encoded data, and change the Content Type to 'application/x-www-form-urlencoded' (you can test with your set Content Type).

  2. Under Actions you'll see several buttons. Create requests are POSTed to the resource, so click Post. Ideally, if all went well, you will see something positive response.

Please let me know if you have any query/concern regarding this.

  • Thanks. I did this with Postman for Google Chrome. When I tried to post a node, I got a 403: Access denied for user anonymous – drupal-newbie Aug 4 '15 at 15:55
  • So, I got it to work in Postman. But not in my script. Even if I send the cookie and token information that I got in Postman through the script, I still get the same 403 error. Thanks for your time. – drupal-newbie Aug 4 '15 at 18:08

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