Domanda

Ho creato un servizio Web nell'utilizzo di Spring Framework in Java e l'ho eseguito su un server TC su LocalHost. Ho testato il servizio Web utilizzando Curl e funziona. In altre parole, questo comando Curl pubblicherà una nuova transazione al servizio 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}'

Ora sto costruendo un'app Web usando ROR e vorrei fare qualcosa di simile. Come posso costruirlo? Fondamentalmente, l'app Web ROR sarà un client che pubblica al servizio Web.

Cercando così e il web, ho trovato alcuni link utili ma non riesco a farlo funzionare. Ad esempio, da questo inviare, usa net/http.

Ho provato ma non funziona. Nel mio controller, ho

  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

Ritorna con errore:

undefined local variable or method `url_path' for #<TransactionsController:0x0000010287ed28>

Il codice di esempio che sto usando proviene qui

Non sono attaccato a Net/HTTP e non mi dispiace usare altri strumenti fintanto che posso svolgere facilmente lo stesso compito.

Grazie mille!

È stato utile?

Soluzione

url = URI.parse('http://localhost:8080/BarcodePayment/transactions/')
response = Net::HTTP::Post.new(url_path)

Il tuo problema è esattamente ciò che l'interprete ti ha detto che è: url_path non è dichiarato. Quello che vuoi è chiamare il metodo #Path sulla variabile URL dichiarata nella riga precedente.

url = URI.parse('http://localhost:8080/BarcodePayment/transactions/')
response = Net::HTTP::Post.new(url.path)

dovrebbe funzionare.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top