Make HTTP request using Java and Curl

Last week I was asked to make some tools that talked to a Rails app. Let say you have a Rails app that has a scaffold name Product and resides on www.your_app.com . If you want to create a new Product, you go to www.your_app.com/products/new, fill in the form in there, including typing in some information product name, choosing a photo to upload and finally hit Create. A new Product will be created. Now how do automate it? I mean, to accomplish the above task without going to the web, fill out the form, select a file to upload and hit Create. Instead, you run some script and voila`, a new Product is created. It sounds confusing at first.

Interestingly, all the user just does is to make some HTTP request, specifically, GET and POST here. So you want you want to do is to write a script that make HTTP request, taking all the information that need to be filled up in the form as parameters, including the photo to be uploaded.

First Try: Use Java. I tried to write some script using Java. A quick research gives me some options: Either use Java.net or Apache HTTPClient. The second one is better. This is a very easy-to-use library to deal with HTTP. So;I have an html form that looks something like this:

<div class="field">
  <input id="product_name" name="product[name]" size="30" type="text"/>
</div>
<div class="field">
  <input id="product_picture" name="product[picture]" size="30" type="file"/>
<div class="actions"><input name="submit" value="Create" size="30" type="submit"/></div>

Java code:;

String fileName = "example.img"
HttpHost host = new HttpHost("your_app.com", 80, "http");
HttpPost httpPost = new HttpPost("/products");
MultipartEntity entity =new MultipartEntity();
FileBody bin =new FileBody(newFile(fileName));
entity.addPart("product[name]", "Product1");entity.addPart("product[picture]", bin);
httpPost.setEntity(entity);
HttpResponse postResponse = httpClient.execute(host, httpPost); 

The piece of code above is pretty much self-explanatory. It first creates a host and an HttpPost object. Then it makes a; MultipartEntity which is what stores all the information that user supposes to fill in the form, including choosing an image file to upload along with the creation of a new Product. Next it assigns the entity to the HttpPost and execute the HTTP Post request;

Now the question is: Is there any other way easier to do this. Digging into stackoverflow, I see that this can be done using CURL – An unix tool that is mainly used to download stuffs on command line interface, like wget.

Second Try: Use CURL. Actually the above code can be done by just one curl command:

curl --data product[name]="Product1" --form product[picture]=@/path/to/file.img --form submit=Create [URL]

The above line feeds in the product name, which is “Product1”, chooses a file to upload and make a HTTP Post request to upload the selected file and create a new Product. Run the the command, refresh the web interface and I see a new product get created :-). Note the path/to/file.img needs a “@” that comes right before it, otherwise the /path/to/file.imge will be interpreted as a string. More tutorials with curl can be found on its official page http://curl.haxx.se/docs/httpscripting.html

Lesson learned: Before doing anything, check if something similar is already out there, ready to be used o_O.

Read More Post

STAY CONNECTED

Most Popular

Why Serviced Apartments Are the Perfect Choice for Long-Term Stays in Singapore – Everything You Need to Know
17 Dec

Why Serviced Apartments Are the Perfect Choice for Long-Term Stays in Singapore – Everything You Need to Know

If you’re heading to Singapore for a few weeks or many months, maybe for work or study or just to

Manifesting Your Dream Career: Finding Your Passion And Purpose
01 Aug

Manifesting Your Dream Career: Finding Your Passion And Purpose

Are you passionate about finding your dream career? Manifesting can help you align your career with your passions and purpose.

Revolutionizing Your Ride: Expert Auto Service At Carolina Auto Service
27 Feb

Revolutionizing Your Ride: Expert Auto Service At Carolina Auto Service

Finding a reliable auto service provider is paramount in the bustling world of vehicle maintenance and repair. That's where Carolina

Floratam: The Robust Grass Solution From Council Growers SOD
25 Jan

Floratam: The Robust Grass Solution From Council Growers SOD

Floratam grass, a popular St. Augustine variety, has become a go-to choice for homeowners and landscapers seeking a lush, durable

Carolina Mobile Autoservice: Your Go-To For Expert Brake Service Repair
25 Jan

Carolina Mobile Autoservice: Your Go-To For Expert Brake Service Repair

For vehicle owners, the braking system is among the most critical components for safety and performance. Carolina Mobile Autoservice, a

Safeguarding Your Property: Expert Water Damage Services By MBC Capital
24 Jan

Safeguarding Your Property: Expert Water Damage Services By MBC Capital

Water damage can significantly impact homes and businesses, often leaving a trail of destruction and disruption in its wake. Understanding