1
我在Java中使用Spring框架構建了Web服務,並使其在localhost上的tc服務器上運行。我使用curl測試了Web服務,它工作正常。換句話說,這個curl命令會向web服務發佈一個新的事務。使用Ruby on Rails將JSON/XML數據發佈到Web服務
curl -X POST -H 'Accept:application/json' -H 'Content-Type: application/json' http://localhost:8080/BarcodePayment/transactions/ --data '{"id":5,"amount":5.0,"paid":true}'
現在,我正在構建一個使用RoR的Web應用程序,並且想要做類似的事情。我該如何建立?基本上,RoR Web應用程序將是一個發佈到Web服務的客戶端。
在SO和網上搜索,我發現了一些有用的鏈接,但是我無法使它工作。例如,從這個post,他/她使用淨/ http。
我試過了,但不起作用。在我的控制,我有
require 'net/http'
require "uri"
def post_webservice
@transaction = Transaction.find(params[:id])
@transaction.update_attribute(:checkout_started, true);
# do a post service to localhost:8080/BarcodePayment/transactions
# use net/http
url = URI.parse('http://localhost:8080/BarcodePayment/transactions/')
response = Net::HTTP::Post.new(url_path)
request.content_type = 'application/json'
request.body = '{"id":5,"amount":5.0,"paid":true}'
response = Net::HTTP.start(url.host, url.port) {|http| http.request(request) }
assert_equal '201 Created', response.get_fields('Status')[0]
end
它與返回錯誤:
undefined local variable or method `url_path' for #<TransactionsController:0x0000010287ed28>
我使用的示例代碼是從here
我沒有連接到網/ http和我不只要我能輕鬆完成相同的任務,就不要介意使用其他工具。
非常感謝!
謝謝,但它不起作用。我不完全是。它不會返回任何錯誤消息,但是Web服務端沒有任何事情發生 – okysabeni