Add body in PATCH request

I’m trying to send a simple HTTPS PATCH request and the body of the network request won’t send. 

I’ve tried to deconstruct the problem away from the server I was using by making a simple local server that accepts an https PATCH request that I’ve tested with Postman and Node, but when I try running the code below in Corona I can get the url parameter, and header parameters, but the body always appears as empty. 

local function networkListener( event ) print("event") if ( event.isError ) then print( "Network error: ", event.response ) else print ( "RESPONSE: " .. event.response ) end end local params = { headers= { ['Cache-Control']='no-cache', headerparam= 'headerValue', ['Content-Type']= 'application/json', ['proxy-connection']= 'keep-alive', ['accept-language']= 'en-US,en;q=0.9', ['accept-encoding']= 'gzip, deflate', accept= 'text/plain', rejectUnauthorized= false, requestCert= true, agent= false }, body= '{"bodyParam":"here"}', } network.request( "https://localhost:8080/patchtest?urlparam=yes", "PATCH", networkListener, params )

I have the same issue. I am using corona SDK 2018.3432 (2018.11.11)

Any solution yet?

ya…

I followed this example as much as possible (https://github.com/coronalabs/plugins-sample-openssl/blob/master/luasec/main.lua)

I had to use the openssl plugin (https://docs.coronalabs.com/plugin/openssl/index.html)

so I could use luasec’s https method which has documentation here: (https://github.com/brunoos/luasec/wiki/LuaSec-0.7),

which only explains what’s different from this documentation (which is what I mostly used): (http://w3.impa.br/~diego/software/luasocket/reference.html).

Here’s what I came out with. (I only need patch with certain parameters so I hard coded a lot of it and just overrode the network api in main) 

mynetwork.lua

local MyNetwork = {} -- local json = require('json') local openssl = require('plugin.openssl') -- local socket = require('socket') -- local http = require('socket.http') local ltn12 = require('ltn12') -- local plugin\_luasec\_ssl = require('plugin\_luasec\_ssl') local plugin\_luasec\_https = require('plugin\_luasec\_https') -- local lfs = require('lfs') MyNetwork.patchRequest = function( params, callback ) local body = params.body or {}--String local headers = params.headers or {} --table local url = params.url --String local output = {} --[[In case of failure, the function returns nil followed by an error message. If successful, the simple form returns the response body as a string, followed by the response status code, the response headers and the response status line. The generic function returns the same information, except the first return value is just the number 1 (the body goes to the sink). if error one: nil two: error Message else one:(String) response body two: response status code responseHeaders: response headers responseStatusLine: response status line]] local one, two, responseHeaders, responseStatusLine = plugin\_luasec\_https.request({ url = url, sink = ltn12.sink.table(output), protocol = "any", method = "PATCH", headers= { ['Cache-Control']='no-cache', -- headerparam= 'headerValue', ['Content-Type']= 'application/json', ['proxy-connection']= 'keep-alive', ['accept-language']= 'en-US,en;q=0.9', -- ['accept-encoding']= 'gzip',--'gzip, deflate',--HAD TO REMOVE FOR FIREBASE!!! ['Content-Length']=string.len(body), accept= 'text/plain', unpack(headers) }, source= ltn12.source.string(body), }) print("responseHeaders") pprint(responseHeaders) print("responseStatusLine") print(responseStatusLine) if one == nil then -- ERROR callback({isError = true, message=two}) else callback({isError = false, response=output}) end end return MyNetwork

main.lua

local advancedNetwork = require("res.advancednetwork") local baseNetworkRequest = network.request network.request = function( url, method, listener, params ) if method == "PATCH" then advancedNetwork.patchRequest(url, listener, params) else baseNetworkRequest( url, method, listener, params ) end end

build.settings

 ["plugin.openssl"] = { publisherId = "com.coronalabs", },

I have the same issue. I am using corona SDK 2018.3432 (2018.11.11)

Any solution yet?

ya…

I followed this example as much as possible (https://github.com/coronalabs/plugins-sample-openssl/blob/master/luasec/main.lua)

I had to use the openssl plugin (https://docs.coronalabs.com/plugin/openssl/index.html)

so I could use luasec’s https method which has documentation here: (https://github.com/brunoos/luasec/wiki/LuaSec-0.7),

which only explains what’s different from this documentation (which is what I mostly used): (http://w3.impa.br/~diego/software/luasocket/reference.html).

Here’s what I came out with. (I only need patch with certain parameters so I hard coded a lot of it and just overrode the network api in main) 

mynetwork.lua

local MyNetwork = {} -- local json = require('json') local openssl = require('plugin.openssl') -- local socket = require('socket') -- local http = require('socket.http') local ltn12 = require('ltn12') -- local plugin\_luasec\_ssl = require('plugin\_luasec\_ssl') local plugin\_luasec\_https = require('plugin\_luasec\_https') -- local lfs = require('lfs') MyNetwork.patchRequest = function( params, callback ) local body = params.body or {}--String local headers = params.headers or {} --table local url = params.url --String local output = {} --[[In case of failure, the function returns nil followed by an error message. If successful, the simple form returns the response body as a string, followed by the response status code, the response headers and the response status line. The generic function returns the same information, except the first return value is just the number 1 (the body goes to the sink). if error one: nil two: error Message else one:(String) response body two: response status code responseHeaders: response headers responseStatusLine: response status line]] local one, two, responseHeaders, responseStatusLine = plugin\_luasec\_https.request({ url = url, sink = ltn12.sink.table(output), protocol = "any", method = "PATCH", headers= { ['Cache-Control']='no-cache', -- headerparam= 'headerValue', ['Content-Type']= 'application/json', ['proxy-connection']= 'keep-alive', ['accept-language']= 'en-US,en;q=0.9', -- ['accept-encoding']= 'gzip',--'gzip, deflate',--HAD TO REMOVE FOR FIREBASE!!! ['Content-Length']=string.len(body), accept= 'text/plain', unpack(headers) }, source= ltn12.source.string(body), }) print("responseHeaders") pprint(responseHeaders) print("responseStatusLine") print(responseStatusLine) if one == nil then -- ERROR callback({isError = true, message=two}) else callback({isError = false, response=output}) end end return MyNetwork

main.lua

local advancedNetwork = require("res.advancednetwork") local baseNetworkRequest = network.request network.request = function( url, method, listener, params ) if method == "PATCH" then advancedNetwork.patchRequest(url, listener, params) else baseNetworkRequest( url, method, listener, params ) end end

build.settings

 ["plugin.openssl"] = { publisherId = "com.coronalabs", },