我正在尝试为以下内容在lua中创建一个多部分的帖子
POST /CMServiceAPI/Record HTTP/1.1
Host: xx.xx.xx.xx
Accept: application/json
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW
Authorization: Basic ddddzxwsdlj3247982323lkjklsdsdlkjsdj==
cache-control: no-cache
Postman-Token: f4442f26-fe43-4ba6-afcc-046ddccbb1bd
Content-Disposition: form-data; name="RecordType"
Document
Content-Disposition: form-data; name="RecordTitle"
Vinay test 2341
Content-Disposition: form-data; name="properties"
RecordNumber
------WebKitFormBoundary7MA4YWxkTrZu0gW--
我将如何在lua中使用它?
我将需要安装luasocket
,希望它对您有用:
local http = require('socket.http')
local ltn12 = require('socket.ltn12')
local body = {}
local boundary = '----WebKitFormBoundary7MA4YWxkTrZu0gW'
local payload = string.format([[
Content-Disposition: form-data; name="RecordType"
%s
Content-Disposition: form-data; name="RecordTitle"
%s
Content-Disposition: form-data; name="properties"
%s
%s
]], 'Document', 'Vinay test 2341', 'RecordNumber', boundary)
local res, code, headers = http.request({
url = 'http://youservernamehere/CMServiceAPI/Record',
method = 'POST',
headers = {
['Content-Type'] = 'multipart/form-data; boundary='..boundary,
['Postman-Token'] = 'f4442f26-fe43-4ba6-afcc-046ddccbb1bd',
Authorization = 'Basic ddddzxwsdlj3247982323lkjklsdsdlkjsdj==',
Accept = 'application/json'
},
source = ltn12.source.string(payload),
sink = ltn12.sink.table(body)
})
print(res)
print(code)
if type(headers) == 'table' then
for k, v in pairs(headers) do
print(k, v)
end
end
print('Response body:')
if type(body) == 'table' then
print(table.concat(body))
else
print('Not a table:', type(body))
end
此代码未经测试,但希望您能理解:)