如何使用文件在cURL中发布json

如何使用文件在cURL中发布json,json,curl,post,lua,Json,Curl,Post,Lua,我试图在Lua中使用cURL发布json并附加多部分文件。按以下方式尝试,但不起作用: local cURL = require "cURL" c = cURL.easy{ url = "http://posttestserver.com/post.php", post = true, httpheader = { "Content-Type: application/json"; }; postfields = "{}"; } c:se

我试图在Lua中使用cURL发布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()

任何帮助都是非常值得的!谢谢大家!

使用
application/json
发布文件是不正确的。在任何语言或库中发布文件时,每次都必须使用
多部分/表单数据

对于您的情况,我建议使用以下示例:

localcurl=require(“cURL”)
c=cURL.easy_init()
c:setopt_url(“http://localhost")
postdata={
--从文件系统发布文件
name={file=“post.lua”,
type=“text/plain”},
--从数据变量发布文件
name2={file=“dummy.html”,
data=“bold”,
type=“text/html”}
c:邮政(邮政数据)
c:执行()
流_postdata={
--从私有读取函数发布文件
name={file=“stream.txt”,
流_length=“5”,
type=“text/plain”}
计数=0
c:post(流_postdata)
c:执行({readfunction=function(n)
如果(计数<5),则
计数=5
返回“流”
终止
归零
结束})
打印(“完成”)

谢谢您的回答!如果我需要为此请求添加json信息。我怎么能做到?关键是我需要发送包含json内部信息的文件。
multipart/formdata
意味着您可以添加任意数量的元素。例如,创建另一个
name\u json=内部有json数据我错了,json数据被附加为HTML文件,我会尽力做到这一点。
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")