Reputation: 536
I need to post to a REST resource by using ruby rest-client
gem.
The example of HTTP request is:
POST /somefolder HTTP/1.1
Authorization: Basic YWFhOmFw
Host: example.com
folder: creat
and it provides a demo code written in php, and works well:
<?php
$process = curl_init('example.com/somefolder');
curl_setopt($process, CURLOPT_POST, 1);
curl_setopt($process, CURLOPT_POSTFIELDS, '');
curl_setopt($process, CURLOPT_HTTPHEADER, array('folder: true'));
curl_setopt($process, CURLOPT_USERPWD, "username:password");
curl_setopt($process, CURLOPT_HEADER, 0);
curl_setopt($process, CURLOPT_TIMEOUT, 30);
curl_setopt($process, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($process, CURLOPT_FOLLOWLOCATION, 1);
print(curl_exec($process)."<br/>");
print(curl_getinfo($process, CURLINFO_HEADER_OUT).'<br/>');
curl_close($process);
?>
My ruby code is:
resource = RestClient::Resource.new('http://example.com/', :user => 'username', :password => 'password')
resource.post 'somefolder', :folder => 'true'
But it returns 403 forbidden. I'd like to know where my ruby is wrong. Thanks.
BTW, how can I capture the HTTP traffic sent by ruby code? I am using MAC.
Upvotes: 1
Views: 280
Reputation: 47923
I'm not the most familiar with Ruby but I think there are two issues with your code. somefolder
is being sent as the content instead of the path and the folder folder
is getting modified. What I think the ruby request will look like is this.
POST / HTTP/1.1
Authorization: Basic YWFhOmFw
Host: example.com
X-Folder: true
somefolder
My reading of the documentation suggest that switching to the following will fix the path but I'm not sure if you can fix the header name client side.
resource = RestClient::Resource.new('http://example.com/', :user => 'username', :password => 'password')
resource['somefolder'].post, :folder => 'true'
You may have to include an empty string or a null value for the content.
Use Charles Proxy to view HTTP requests.
Upvotes: 1