我目前正试图通过POST请求将csv文件发送到数据库,但每次我发送POST请求时请求体为空。我试着把标题切换到"application/json",但是我收到一个格式错误,我不知道如何补救。
代码如下:
Sub macroPOST()
Dim filePath As String
filePath = "some file path"
Set objHTTP = CreateObject("MSXML2.XMLHTTP")
Url = "some url"
With objHTTP
.Open "POST", Url, False
.SetRequestHeader "Content-Type", "text/csv"
.SetRequestHeader "Accept", "application/xml"
.Send filePath
End With
End Sub
try
Sub macroPOST()
Dim objHTTP, FSO As Object, TS As Object
Const URL = "some url"
Const filePath = "some path"
Set objHTTP = CreateObject("MSXML2.XMLHTTP")
Set FSO = CreateObject("Scripting.FileSystemObject")
If FSO.FileExists(filePath) Then
Set TS = FSO.opentextfile(filePath, 1)
With objHTTP
.Open "POST", URL, False
.SetRequestHeader "Content-Type", "text/csv"
.Send TS.Readall
TS.Close
'Debug.Print objHTTP.Status, objHTTP.StatusText, objHTTP.responseText
End With
Else
MsgBox "'" & filePath & "' not found", vbCritical, "File not found"
End If
End Sub
使用node.js服务器测试的代码
const http = require('http');
var server = http.createServer(function(request, response) {
if (request.method == 'POST') {
var body = '';
request.on('data', function (data) { body += data });
request.on('end', function () {
try {
response.writeHead(200, {"Content-Type": "text/plain"});
response.end(body);
return;
} catch (err){
response.writeHead(500, {"Content-Type": "text/plain"});
response.end("Error");
return;
}});
}});
server.listen(3000);
console.log("listening on 3000")