โ ๏ธ Warning: This post is over a year old, the information may be out of date.
๐ Sending JSON data file using CURL to API endpoint
๐ | โฐ 2 minutes
I developed warehouse system and use restful as communication method between our software and client software. We let end point of our system to talk each other with API designed.
For simple and small test using Postman or SOAP-UI is enough for me but to when to test this system with massive data via API is quite headache, plus the mock features on Postman limited and not “mocking” enough as I want.
Lucky enough, I am good with unit test (self claimed 😆) so since the system are develop using java, then I use Junit as helper to help me do the automation test. It look nice when test it locally but somehow I still have an issue to remote test using Junit on my Eclipse IDE. It all because the remote server we connnecting is on customer premise and the connection are so bad! 😤
As a guy who love the old school trick, I use curl
as the best and simple solution to POST
the data into system API endpoint and I was right, the server much responsive to handle data.
Here I share how I using curl
to POST data to our API endpoint locally (ssh to server and run the command inside). First of all, i will generated json
file to use as data driven unit test and send to my server via scp
:
$ scp /C/Users/Robbi/Desktop/TEST_CASE/*.json [email protected]:/tmp/test
when finished transfer the files, just ssh
into server and navigate where the data are stored.
Let see, here I have 212,717 json
file that I want this use with API endpoint. (Tips: All filename must are unique and properly sorted, so you can trace if you unit test failed)
$ find . -type f | wc -l
212717
How to POST
json data file to API endpoint?
If I want to POST a single data from my file, I just need to send command like this from directory that store my data file:
$ curl -X POST -H "Content-Type: application/json" -d @TC-00001.json http://localhost:8182/order
If I want to POST all the data from my file, I simply just need to execute this looping
command inside directory that store my data files:
$ for f in TC-*.json; do printf "\nLoad ${f} - " && curl -X POST -H "Content-Type: application/json" --data @${f} http://localhost:8182/order;done
Simple right? As long you can generate json
file then you can use this technique todo data driven test your system endpoint :)
Posted by: Hugo