Skip to content
Advertisement

Executing HTTP POST using curl in bash with environment variables as data

I am trying to make a HTTP POST to a server using curl but I can’t seem to make it work.

The server has the Content-Type set to application/json so I need to send a json object to the server. My object looks something like this

[ { "timestamp" : 1480938623, "noProc" : 2, "cpuLoad" : 4.12331, "usedSpace" : 6.10906, "usedRam" : 9.71923, "usedJavaRam" : 7.9, "cpuLoads" : [ 4.14741, 4.09922 ] }, { "timestamp" : 1480938624, "noProc" : 2, "cpuLoad" : 4.1233, "usedSpace" : 6.10907, "usedRam" : 9.7014, "usedJavaRam" : 7.9, "cpuLoads" : [ 4.14741, 4.09922 ] }, { "timestamp" : 1480938625, "noProc" : 2, "cpuLoad" : 4.12331, "usedSpace" : 6.10907, "usedRam" : 9.69993, "usedJavaRam" : 7.9, "cpuLoads" : [ 4.14742, 4.09922 ] }, { "timestamp" : 1480938626, "noProc" : 2, "cpuLoad" : 4.1233, "usedSpace" : 6.10907, "usedRam" : 9.70316, "usedJavaRam" : 7.9, "cpuLoads" : [ 4.14741, 4.09922 ] }, { "timestamp" : 1480938627, "noProc" : 2, "cpuLoad" : 4.12331, "usedSpace" : 6.10907, "usedRam" : 9.69905, "usedJavaRam" : 7.9, "cpuLoads" : [ 4.14742, 4.09922 ] }, { "timestamp" : 1480938628, "noProc" : 2, "cpuLoad" : 4.1233, "usedSpace" : 6.10907, "usedRam" : 9.70091, "usedJavaRam" : 7.9, "cpuLoads" : [ 4.14741, 4.09922 ] }, { "timestamp" : 1480938629, "noProc" : 2, "cpuLoad" : 4.12331, "usedSpace" : 6.10907, "usedRam" : 9.6992, "usedJavaRam" : 7.9, "cpuLoads" : [ 4.14742, 4.09922 ] }, { "timestamp" : 1480938630, "noProc" : 2, "cpuLoad" : 4.12331, "usedSpace" : 6.10907, "usedRam" : 9.70263, "usedJavaRam" : 7.9, "cpuLoads" : [ 4.14741, 4.09922 ] }, { "timestamp" : 1480938631, "noProc" : 2, "cpuLoad" : 4.12331, "usedSpace" : 6.10907, "usedRam" : 9.6988, "usedJavaRam" : 7.9, "cpuLoads" : [ 4.14742, 4.09922 ] }, { "timestamp" : 1480938632, "noProc" : 2, "cpuLoad" : 4.12331, "usedSpace" : 6.10907, "usedRam" : 9.6992, "usedJavaRam" : 7.9, "cpuLoads" : [ 4.14742, 4.09922 ] } ]

If I try to use the following command it works

curl -k -X POST -H "Content-Type: application/json" -d '[ { "timestamp" : 1480938623, "noProc" : 2, "cpuLoad" : 4.12331, "usedSpace" : 6.10906, "usedRam" : 9.71923, "usedJavaRam" : 7.9, "cpuLoads" : [ 4.14741, 4.09922 ] }, { "timestamp" : 1480938624, "noProc" : 2, "cpuLoad" : 4.1233, "usedSpace" : 6.10907, "usedRam" : 9.7014, "usedJavaRam" : 7.9, "cpuLoads" : [ 4.14741, 4.09922 ] }, { "timestamp" : 1480938625, "noProc" : 2, "cpuLoad" : 4.12331, "usedSpace" : 6.10907, "usedRam" : 9.69993, "usedJavaRam" : 7.9, "cpuLoads" : [ 4.14742, 4.09922 ] }, { "timestamp" : 1480938626, "noProc" : 2, "cpuLoad" : 4.1233, "usedSpace" : 6.10907, "usedRam" : 9.70316, "usedJavaRam" : 7.9, "cpuLoads" : [ 4.14741, 4.09922 ] }, { "timestamp" : 1480938627, "noProc" : 2, "cpuLoad" : 4.12331, "usedSpace" : 6.10907, "usedRam" : 9.69905, "usedJavaRam" : 7.9, "cpuLoads" : [ 4.14742, 4.09922 ] }, { "timestamp" : 1480938628, "noProc" : 2, "cpuLoad" : 4.1233, "usedSpace" : 6.10907, "usedRam" : 9.70091, "usedJavaRam" : 7.9, "cpuLoads" : [ 4.14741, 4.09922 ] }, { "timestamp" : 1480938629, "noProc" : 2, "cpuLoad" : 4.12331, "usedSpace" : 6.10907, "usedRam" : 9.6992, "usedJavaRam" : 7.9, "cpuLoads" : [ 4.14742, 4.09922 ] }, { "timestamp" : 1480938630, "noProc" : 2, "cpuLoad" : 4.12331, "usedSpace" : 6.10907, "usedRam" : 9.70263, "usedJavaRam" : 7.9, "cpuLoads" : [ 4.14741, 4.09922 ] }, { "timestamp" : 1480938631, "noProc" : 2, "cpuLoad" : 4.12331, "usedSpace" : 6.10907, "usedRam" : 9.6988, "usedJavaRam" : 7.9, "cpuLoads" : [ 4.14742, 4.09922 ] }, { "timestamp" : 1480938632, "noProc" : 2, "cpuLoad" : 4.12331, "usedSpace" : 6.10907, "usedRam" : 9.6992, "usedJavaRam" : 7.9, "cpuLoads" : [ 4.14742, 4.09922 ] } ]' https://10.241.53.185/rest/monitor

But if I store this JSON object into an environment variable, let’s name it JSON_DATA and use the following command

curl -k -X POST -H "Content-Type: application/json" -d $JSON_DATA https://10.241.53.185/rest/monitor

I am receiving various errors related to unmatched braces and a 400 Bad Request

{"timestamp":1480949788856,"status":400,"error":"Bad Request","exception":"org.springframework.http.converter.HttpMessageNotReadableException","message":"Could not read document: Unexpected end-of-input: expected close marker for Array (start marker at [Source: java.io.PushbackInputStream@756cb7d; line: 1, column: 1])n at [Source: java.io.PushbackInputStream@756cb7d; line: 1, column: 3]; nested exception is com.fasterxml.jackson.core.io.JsonEOFException: Unexpected end-of-input: expected close marker for Array (start marker at [Source: java.io.PushbackInputStream@756cb7d; line: 1, column: 1])n at [Source: java.io.PushbackInputStream@756cb7d; line: 1, column: 3]","path":"/rest/monitor"}

Could you please help me resolve this issue?

Thanks

Advertisement

Answer

Try quoting it: -d "$JSON_DATA" – anubhava

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement