Note that there are some explanatory texts on larger screens.

plurals
  1. POJava: HTTP Post to create new “Ride” in a Ruby on Rails application
    primarykey
    data
    text
    <p>My question is very similar to <a href="https://stackoverflow.com/questions/695971/java-http-post-to-create-new-product-in-a-ruby-on-rails-application">Java: HTTP Post to create new &quot;Product&quot; in a Ruby on Rails application</a></p> <p>I have a form that I want to "post" to a form that looks like this</p> <pre><code>Offer:&lt;br /&gt; &lt;input id="ride_togive" name="ride[togive]" size="30" type="text" /&gt; &lt;/p&gt; &lt;p&gt; Request for Offer:&lt;br /&gt; &lt;input id="ride_totake" name="ride[totake]" size="30" type="text" /&gt; &lt;/p&gt; </code></pre> <p>My Java code looks like this</p> <pre><code>DefaultHttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost("http://172.16.60.129:3000/rides"); // Configure the form parameters List &lt;NameValuePair&gt; nvps = new ArrayList &lt;NameValuePair&gt;(); nvps.add(new BasicNameValuePair("ride_totake", "Ride to Vail")); nvps.add(new BasicNameValuePair("ride_togive", "$20")); post.addHeader("Content-Type","application/json"); try { post.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8)); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } HttpResponse response = null; try { response = client.execute(post); } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } HttpEntity entity = response.getEntity(); System.out.println("form get: " + response.getStatusLine()); if (entity != null) { try { entity.consumeContent(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } </code></pre> <p>Here is the output of the RoR console</p> <pre><code>Processing RidesController#create (for 172.16.60.1 at 2009-11-04 22:22:52) [POST] Parameters: {"_json"=&gt;"totake=Ride+to+Vail&amp;togive=%2420"} Ride Columns (0.6ms) SHOW FIELDS FROM `rides` SQL (0.1ms) BEGIN Ride Create (0.3ms) INSERT INTO `rides` (`isoffer`, `togive`, `howlong`, `updated_at`, `totake`, `created_at`) VALUES(NULL, NULL, 0, '2009-11-05 05:22:52', NULL, '2009-11-05 05:22:52') SQL (0.0ms) COMMIT Redirected to http://172.16.60.129:3000/rides Completed in 9ms (DB: 1) | 302 Found [http://172.16.60.129/rides] </code></pre> <p>I guess my real question is how should I be addressing the RoR variables in Java?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload