📢 Actions Speak Louder Than Words!

Sending JSON data file using CURL to API endpoint

Posted: Nov 30, 2020 | Reading time: 2 min

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 :satisfied:) 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! :triumph:

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 sayaComel@

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

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 :)


Discussion and feedback

You can use utterances provided below to post comment on behalf using Github account. Alternatively, you can just send a (plain text) email to my private email or personal public mailing list. In a few cases and on certain time, I just don’t have time to moderate them. Please read terms-of-service (ToS) for details.