mod_curl POST Request responds with error from LUA script

Status
Not open for further replies.

Seglaf

New Member
Jan 21, 2019
8
1
3
31
Hello! I'm trying to send a POST request in a Lua script after a session has ended in Freeswitch. I am fairly new to using the mod_curl application and have included my research sources below.

I've worked out most of the implementation but I'm stuck being unable to send a POST request successfully via the mod_curl app. I've confirmed my API info, url, and data is correct and I'm able to successfully run this command in the terminal of my test server. -

curl -X POST https://example.app/api/send -H "Content-Type: application/json" -H "Authorization: 'Bearer xxxTokenxxx:xxxAccountUUIDxxx" -d '{"to":"+15622133497", "from":"+19494324054", "message":"Hello world, you are beautiful."}'

There's no problem when running this or any other external POST request tests. The issue is when using the mod_curl application in Lua I can't seem to get the request to clear.

Code:
--Lua Code:

api = freeswitch.API();

postStr = "https://example.app/api/send append_headers 'Authorization: Bearer xxxTokenxxx:xxxAccountUUIDxxx' post to='+15622133497'&from='+19494324054'&message='Hello world, you are beautiful.'";

reply = api:execute("curl", postStr);

freeswitch.consoleLog("INFO", reply .. "\n")

I've confirmed that doing a GET request works no problem and the POST request is being received, but the body data is being rejected.

I'm using fs_cli to see any responses or errors but do not have access to the API server I'm calling, so I'm not sure what they are seeing when I request via mod_curl.

The response I keep getting is a rejection for "{"error":"Invalid from number."}". To fix this I've tried changing the from value to match the urlencoded format as well as many other approaches -

from='+19494324054' from=+19494324054 (no quotes) from=%2B19494324054 (replace '+' with url code) etc..

I've looked through all the documents I could find online but nothing explains exactly how this should be formatted besides (value1=value2&value3=value4).

I'm certain this response is from the API server I'm calling as I've dealt with it in the past. The issue is just a data formatting issue as the data I'm trying to pass is exactly how it should be in the working curl terminal command above. How do I properly format this data to be used in the mod_curl application api:execute("curl", postStr)?




Sources that helped me so far -
 
Last edited:

Seglaf

New Member
Jan 21, 2019
8
1
3
31
Hello! I'm trying to send a POST request in a Lua script after a session has ended in Freeswitch. I am fairly new to using the mod_curl application and have included my research sources below.

I've worked out most of the implementation but I'm stuck being unable to send a POST request successfully via the mod_curl app. I've confirmed my API info, url, and data is correct and I'm able to successfully run this command in the terminal of my test server. -

curl -X POST https://example.app/api/send -H "Content-Type: application/json" -H "Authorization: 'Bearer xxxTokenxxx:xxxAccountUUIDxxx" -d '{"to":"+15622133497", "from":"+19494324054", "message":"Hello world, you are beautiful."}'

There's no problem when running this or any other external POST request tests. The issue is when using the mod_curl application in Lua I can't seem to get the request to clear.

Code:
--Lua Code:

api = freeswitch.API();

postStr = "https://example.app/api/send append_headers 'Authorization: Bearer xxxTokenxxx:xxxAccountUUIDxxx' post to='+15622133497'&from='+19494324054'&message='Hello world, you are beautiful.'";

reply = api:execute("curl", postStr);

freeswitch.consoleLog("INFO", reply .. "\n")

I've confirmed that doing a GET request works no problem and the POST request is being received, but the body data is being rejected.

I'm using fs_cli to see any responses or errors but do not have access to the API server I'm calling, so I'm not sure what they are seeing when I request via mod_curl.

The response I keep getting is a rejection for "{"error":"Invalid from number."}". To fix this I've tried changing the from value to match the urlencoded format as well as many other approaches -

from='+19494324054' from=+19494324054 (no quotes) from=%2B19494324054 (replace '+' with url code) etc..

I've looked through all the documents I could find online but nothing explains exactly how this should be formatted besides (value1=value2&value3=value4).

I'm certain this response is from the API server I'm calling as I've dealt with it in the past. The issue is just a data formatting issue as the data I'm trying to pass is exactly how it should be in the working curl terminal command above. How do I properly format this data to be used in the mod_curl application api:execute("curl", postStr)?




Sources that helped me so far -

Annnd I was able to figure this out -

Code:
jsonStr = "{\"from\":\"+19494324054\",\"to\":\"+15622133497\",\"message\":\"Hello%20World\"}";

postStr = "https://example.app/api/send append_headers 'Authorization: Bearer xxxTokenxxx:xxxAccountUUIDxxx' Content-Type 'application/JSON' post " .. jsonStr;

reply = api:execute("curl", postStr);

freeswitch.consoleLog("INFO", "RESPONSE TIME ================ " .. reply .. "\n")

This was after finding this old thread that had some json formatting in regards to json data for mod_curl. -
 
Status
Not open for further replies.