2017-03-03 161 views
0

我想發佈json使用cURL在Lua並附加多部分文件。嘗試以下方式,但它不起作用:如何發佈json與捲曲文件

local cURL = require "cURL" 

c = cURL.easy{ 
    url  = "http://posttestserver.com/post.php", 
    post  = true, 
    httpheader = { 
    "Content-Type: application/json"; 
    }; 
    postfields = "{}"; 
} 

c:setopt_httppost(curl.form() 
       :add_file('file', recording_filename, 'audio/mp4', 
         filename..'.mp4', {'Content-length: ' .. fileSize} 
      )) 

c:perform() 

任何幫助將是非常可觀的!謝謝!

+0

我錯了JSON數據連接爲HTML文件,我會盡力使這項工作。 – os11k

回答

1

這是不正確的使用application/json張貼文件。每次使用任何語言或圖書館發佈文件時,您都必須使用multipart/form-data

對於你的情況,我會usggest使用本示例:

https://github.com/Lua-cURL/Lua-cURLv2/blob/master/examples/post.lua

local cURL = require("cURL") 

c = cURL.easy_init() 

c:setopt_url("http://localhost") 
postdata = { 
    -- post file from filesystem 
    name = {file="post.lua", 
     type="text/plain"}, 
    -- post file from data variable 
    name2 = {file="dummy.html", 
     data="<html><bold>bold</bold></html>", 
     type="text/html"}} 
c:post(postdata) 
c:perform() 

stream_postdata = { 
    -- post file from private read function 
    name = {file="stream.txt", 
     stream_length="5", 
     type="text/plain"}} 

count = 0 
c:post(stream_postdata) 
c:perform({readfunction=function(n) 
       if (count < 5) then 
        count = 5 
        return "stream" 
       end 
       return nil 
      end}) 
print("Done") 
+0

謝謝你的回答!如果我需要爲此請求添加json信息。我如何做到這一點?關鍵是我需要發送帶有json內部信息的文件。 – os11k

+0

'multipart/form-data'意味着您可以添加儘可能多的元素。例如,在裏面創建另一個帶有json數據的'name_json = ...' – Vyacheslav