Http POST request problems (SOLVED)

Trying to hook up Defold with the POEditor ( a nifty localization tool).
Sending a normal Curl request with the same arguments it works fine and gives me the response I want but trying the same thing in Defold I’m getting error response that no data was sent using the POST method.
This is what I’m doing:

local header = { ["Content-Type"] =  "application/json" }

function M.download_language(lang)
	local args = {
		api_token = token,
		id = project,
		action = "export",
		type = "key_value_json",
		language = lang,
	}
	M.post(args,function(a,b,c,d) pprint(a) pprint(b) pprint(c) pprint(d) end )
end

function M.post(args, cb)
	if type(args) == "table" then 
		args = json.encode(args)
	end
	
	http.request("https://poeditor.com/api/", "POST", 
		function (self,id,result,sender)
			cb(self,id,result,sender) 
		end, header, args)
end

results:
{
status = 200,
response = {“response”:{“status”:“fail”,“code”:“4012”,“message”:“No data sent using POST method”}},
headers = {
x-powered-by = PHP/5.5.9-1ubuntu4.20,
expires = Thu, 19 Nov 1981 08:52:00 GMT,
content-type = application/json; charset=utf-8,
content-length = 87,
cache-control = no-store, no-cache, must-revalidate, post-check=0, pre-check=0,
pragma = no-cache,
date = Sat, 17 Dec 2016 12:20:39 GMT,
server = Apache/2.4.7 (Ubuntu),
set-cookie = PHPSESSID=m2vkh7qlhdemcc3gi5154krtb0; path=/,
}
}

Have tried everything but no luck. Any suggestions?

1 Like

If printing header and args they seems good (I assume header must be a table and args a json encoded string)

header (of type table) = {
Content-Type = application/json,
}
args (of type string) = {“id”:“xxxxx”,“type”:“key_value_json”,“action”:“export”,“api_token”:“5cf3xxxxxxxxxxxxxxxxxxxe71ab”,“language”:“se”}

1 Like

Good tip about that API, seems quite interesting!

I’ve tried the api myself now, and it doesn’t use json as “input” format (it doesn’t mention it anywhere where I could find it), but rather url encoded key/value pairs. You also see this if you add -v to your curl request which they provide.

Here’s code that works for me.

local function http_result(self, id, response)
    print(response.status)
    print(response.response)
    pprint(response.headers)
end

function init(self)
    local token = ...
    local headers = {}
    headers["Content-Type"] = "application/x-www-form-urlencoded"
    local args = string.format("api_token=%s&action=list_projects", token)
        
    http.request("https://poeditor.com/api/", "POST", http_result, headers, args)
end
3 Likes

Thanks Mathias! Works great

Yes, Poeditor seems really nifty and could probably be a great tool for many indie devs out there.

1 Like

posting requests also works with various online API Clients, such as ReqBin - by sending REST, SOAP, and WEB API requests to the server right from your browser.