Java で Spring フレームワークを使用して Web サービスを構築し、localhost の tc サーバーで実行しました。curl を使用して Web サービスをテストしたところ、動作しました。つまり、この curl コマンドは新しいトランザクションを 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とWebを検索すると、役立つリンクがいくつか見つかりましたが、機能しません。たとえば、この投稿から、彼/彼女は net/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からです
私は net/http に執着しておらず、同じタスクを簡単に達成できる限り、他のツールを使用してもかまいません。
どうもありがとう!