Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Add body in PATCH request
Started by meganreiffer Sep 30 2018 05:05 AM

2 replies to this topic
network patch
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

meganreiffer

[GLOBAL: userInfoPane.html]
meganreiffer
  • Observer

  • 3 posts
  • Corona SDK

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 )


[TOPIC: post.html]
#2

apps30

[GLOBAL: userInfoPane.html]
apps30
  • Enthusiast

  • 56 posts
  • Corona SDK

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

 

Any solution yet?



[TOPIC: post.html]
#3

meganreiffer

[GLOBAL: userInfoPane.html]
meganreiffer
  • Observer

  • 3 posts
  • Corona SDK

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",
        },



[topic_controls]
[/topic_controls]

Also tagged with one or more of these keywords: network, patch