2017-03-03 73 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