curl JSON POST. What is the right format?

Status
Not open for further replies.

ewdpb

Member
Oct 3, 2019
151
19
18
Hi all,

I realize this is a pure freeswitch question but I have not been able to find a good forum for freeswitch. The SignalWire slack channel is just like shouting in the middle of the street hoping someone will look at you. Anyway, sorry, just venting! the actual question is: I am trying to run a simple http POST with JSON content request using curl.

Code:
myJSON = {\"k1\": v1, \"k2\": v2}
url = 'https://myapiserver.com/api/data content-type \'application/json\' post myJSON';
session:execute('curl', url);

However, I always get a HTTP 0 error code.

It runs without issues when I run it from the server console as:

Code:
curl -d '{"k1": v1, "k2": v2}' -H "Content-Type: application/json" -X POST https://myapiserver.com/api/data

All I can guess is that I am doing it wrong. But I do not know what exactly is it that I am doiing wrong. I have try not escaping the JSON quotes but then I get a format error, which sounds fair.

I am following the docs and some old threads I found but no indication of what the problem may be.

Any idea is very welcome.

Thanks!
 

ewdpb

Member
Oct 3, 2019
151
19
18
So, I found a workable solution for me. I am posting it here in case it helps anyone else:

Code:
cmd = 'curl -X POST \"https://myapiserver.com/api/data\" -H \"Content-Type: application/json\" -d \'' .. myJSON .. '\'';
    local handle = io.popen(cmd)
    local result = handle:read("*a")
    handle:close()

I have no idea if it is the most efficient way of doing this but it does the job. Any suggestion is welcome.

Thanks!
 
Status
Not open for further replies.