Can't upload json file with network.upload

Hello everyone!

I found problem while developing client app: I can’t upload file with json inside. My server shows that there was a post request but can’t get the request’s body. Recently I tried to make this request with network.request but it doesn’t work either. Also callback for request haven’t been called at all. 

Here is gist with node.js server:

[gist]5c64cfe1ca4847f01d54[/gist]

Here is function where I make uploading:

[lua]

local function post(data)

    local headers = {}

    local jsonString = json.encode(data)

    print("I’m posting: " … jsonString)

    local path = system.pathForFile(“data.json”, system.DocumentsDirectory)

    local file = io.open(path, “w”)

    file:write(jsonString)

    io.close( file )

    file = nil

    network.upload(

      url,

      “POST”,

      networkListener,

      “data.json”,

      system.DocumentsDirectory,

      “application/json”

    )

end

[/lua]

Please suggest ways to solve this problem.

The network.upload() isn’t designed to work with POST as it doesn’t construct the typical header/body structure that POST looks for.  Its more for use with the PUT verb and writing the data straight to the data steam.  You would be better off to use network.request() with POST,  making sure to URL encode your JSON string and add it to the request’s body block.

Rob

Thanks for answer, Rob!

Unfortunately, this doesn’t work for me. I tried many ways, changed Content-Type header, transform my data in other forms, but had the same result. Even example from Corona documentation doesn’t work - server  shows that there was request but does not receive its body.

So, because my data is relatively small, I decided to use GET. GET works fine.

The network.upload() isn’t designed to work with POST as it doesn’t construct the typical header/body structure that POST looks for.  Its more for use with the PUT verb and writing the data straight to the data steam.  You would be better off to use network.request() with POST,  making sure to URL encode your JSON string and add it to the request’s body block.

Rob

Thanks for answer, Rob!

Unfortunately, this doesn’t work for me. I tried many ways, changed Content-Type header, transform my data in other forms, but had the same result. Even example from Corona documentation doesn’t work - server  shows that there was request but does not receive its body.

So, because my data is relatively small, I decided to use GET. GET works fine.