簡體   English   中英

通過帶有 vba 的 POST 請求發送 csv 文件

[英]Sending a csv file via POST request with vba

我目前正在嘗試通過 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

嘗試

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")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM