2010-10-29 17 views
0

我想用Ruby代碼調用我的Rails應用程序。ruby​​代碼和Rails應用程序的JAva代碼的發佈請求

我有2個表。

相關博客文章(ID,姓名,蛞蝓,DESC) 評論(ID,數據,NODE_ID,NODE_TYPE)

餘米嘗試後通過我的Ruby代碼中的註釋。

的URL對我來說就像 http://localhost:3000/api/blogs/comment.xml?slug=blogtitle-0&comment=aaaaaaaaaaaaaaa

我不知道怎麼寫的Ruby郵編這一點。

的一個嘗試是

require 'net/http' 
require 'uri' 

url = URI.parse('http://localhost:3000/api/blogs/comment.xml?slug=blogtitle-0') 
req = Net::HTTP::Post.new(url.path) 
req.basic_auth 'aruna', 'aruna' 
req.set_form_data({'comment[data]'=>'aaaaaaaaaaaaaaa'}, ';') 
res = Net::HTTP.new(url.host, url.port).start {|http| http.request(req) } 
case res 
    when Net::HTTPSuccess, Net::HTTPRedirection 
    puts res.body 
    else 
    res.error! 
    end 
end 

在解決這個

編輯請幫忙:同樣的事情,我使用Java中使用新澤西州的庫文件嘗試。

這裏我也沒有得到結果。 一個我在Java中嘗試是,

blogBean = objBlogWrapper.postComment(slug,comment); 

public BlogBean postComment(String slug, String comment) { 
    System.out.println(slug+""+comment); 
    // Create a multivalued map to store the parameters to be send in the REST call 
    MultivaluedMap<String, String> newBlogParam = new MultivaluedMapImpl(); 
    // newBlogParam.add("blogpost[slug]", slug); 
    // newBlogParam.add("comment[data]", comment); 
    newBlogParam.add("slug", slug); 
    newBlogParam.add("comment", comment); 

    BlogBean blogBean = null; 
    try {  
     blogBean = webResource.path(ConfigurationUtil.POST_COMMENT).header(ConfigurationUtil.AUTHENTICATION_HEADER, authentication) 
.type(MediaType.APPLICATION_FORM_URLENCODED_TYPE).accept(MediaType.APPLICATION_XML_TYPE).post(BlogBean.class, newBlogParam); 

    }catch (UniformInterfaceException uie) { 
     if (uie.getResponse().getStatus() == 401) { 
      System.out.println("Can not authenticate user "+ConfigurationUtil.userName + 
          ". Please check your username/password and try again."); 
     } else { 
      System.out.println("Error when trying to talk to app. " + 
          "HTTP status code "+uie.getResponse().getStatus()+"returned. Finishing."); 

     } 
     return null; 
    } catch (ClientHandlerException che) { 
     System.out.println("Error when trying to talk toapp. Network issues? " + 
         "Please check the following message and try again later: "+che.getMessage()); 
     return null; 
    } 
    return blogBean; 
} 

在我的ConfigurationUtil.POST_COMMENT = 「API /博客/ comment.xml」;

以上犯規給我任何錯誤,也沒有發佈評論..

請給點建議。

回答

1

隨着Faraday你只是做

body_resp = Faraday.post 'http://localhost:3000/api/blogs/comment.xml', {:slug => 'blogtitle-0', :comment => 'aaaaaaaaaaaaaaa'}.body 

使用一些外部的寶石可以提供幫助,因爲網:: HTTP的Ruby類是不是最簡單的API。

1

在這裏你去,

host, phost = ['http://localhost:3000', 'http://proxyhost:2521' ] 
    user, password = [ 'user_name' , 'password' ] 

    url, p_url = [URI.parse(host), URI.parse(phost)] 
    resp = nil 

    http = Net::HTTP.new(url.host, url.port, p_url.host, p_url.port) 

    req = Net::HTTP::Post.new(url.path) 
    req.basic_auth user, password 
    req.set_content_type 'application/xml' 

    req.body = 'your_request_body' 
    resp = http.start{|h| h.request(req)} 

你可能不需要代理主機都沒有。但是,如果你希望你的請求代理通過,你可以使用它。 resp對象會有你的迴應。

相關問題