Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

API - multipart/form-data

Status
Not open for further replies.

Swi

Programmer
Feb 4, 2002
1,963
0
36
US
Hi,

I have the following curl but I've never posted a CSV file to a REST API this way. I am stuck on the part shown below. Any ideas?

Code:
curl --request POST     "https:/xxxxxxxx.com/api/reports/upload"     --header "Authorization: Bearer {YOUR_AUTH_KEY}"     --header "Content-Type: multipart/form-data"     --header "Accept: application/json"     --form "file=@/tmp/phpgXXXXX"

VB (so far):
    url = "[URL unfurl="true"]https://xxxxxxxx.com/api/reports/upload"[/URL]
    Set HttpReq = New MSXML2.XMLHTTP60
    HttpReq.Open "POST", url, False
    HttpReq.setRequestHeader "Authorization", token
    HttpReq.setRequestHeader "Content-Type", "multipart/form-data"
    [b]<-----What to put here----->[/b]
    HttpReq.send (jsontxt)

Swi
 
I found this online but still no luck. BAsically it return a 200 success but if I check the responsetext it is not a response but the whole webpage of the API. Any ideas?

Code:
Sub UploadFile()
    Dim url As String
    Dim filePath As String
    Dim fileContent As String
    Dim boundary As String
    Dim token As String
    Dim HttpReq As Object
    Dim data As String
    Dim fileData As String
    Dim stream As Object
 
    ' Set the URL and token
    url = "[URL unfurl="true"]https://xxxxxxxx.com/api/reports/upload"[/URL]
    token = "Your_Authorization_Token"
    filePath = "C:\path\to\your\file.txt" ' Replace with the path to your file
 
    ' Create the boundary string
    boundary = "----WebKitFormBoundary" & Format(Now(), "yyyymmddhhnnss")
 
    ' Read the file content into a string
    Set stream = CreateObject("ADODB.Stream")
    stream.Type = 1 ' adTypeBinary
    stream.Open
    stream.LoadFromFile filePath
    fileData = stream.Read
    stream.Close
 
    ' Create the multipart form data
    data = "--" & boundary & vbCrLf
    data = data & "Content-Disposition: form-data; name=""file""; filename=""" & Mid(filePath, InStrRev(filePath, "\") + 1) & """" & vbCrLf
    data = data & "Content-Type: application/octet-stream" & vbCrLf & vbCrLf
    data = data & fileData & vbCrLf
    data = data & "--" & boundary & "--"
 
    ' Create the XMLHTTP object and set up the request
    Set HttpReq = CreateObject("MSXML2.XMLHTTP60")
    HttpReq.Open "POST", url, False
    HttpReq.setRequestHeader "Authorization", "Bearer " & token
    HttpReq.setRequestHeader "Content-Type", "multipart/form-data; boundary=" & boundary
 
    ' Send the request
    HttpReq.send data
 
    ' Handle the response
    If HttpReq.Status = 200 Then
        MsgBox "File uploaded successfully"
    Else
        MsgBox "Error: " & HttpReq.Status & " - " & HttpReq.statusText
    End If
End Sub

Swi
 
The problem here is that you can make a perfectly valid HTTP POST, but the receiving end API does not like it ...

In other words, without you giving the API (and possibly a test site to try against) we cannot properly help.

What I can say is that your second example is somewhat over complicated for a simple CSV send ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top