Say if you want to debug a API call that your application makes to a external API. And also assume you are using rails and ActiveResource to make your API calls.
You will need to enable debugging in Net:HTTP to actually see the HTTP requests that are being made by ActiveResource. And then if you suspect that this is something wrong with the API, you’ll probably have to convert those requests in to curl commands so you can file a bug report with the guys who are responsible for the API.
Recently i had to do what i just described above. And i am documenting it down so i can reference it in the future. Making up curl commands fades from my memory so fast, i really need to document this stuff :)
Printing out the HTTP requests made by ActiveResource
ActiveResource uses Net::HTTP internally to make all the http requests. So what we can do is to monkey patch ActiveResource to enable debugging on Net::HTTP.
I choose to add a initializer that enables debugging.
1 2 3 4 5 6 7 8 9 10 11 12 13
After adding this you should start seeing HTTP requests in your logs whenever you make a API call
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
Converting the HTTP requests to CURL commands
Now that you know what the HTTP request looks like and what exact params and values are passed to the API call you can construct the CURL command as following. Note that I am looking at a update action which uses the http method PUT to save a edit to a resource (its a RESTful API) in the server.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
Note that we are sending the input as json. When you have a object with a lot of properties and values this is the way to send things via curl. I am sending a Identity object with all fields and values set.