简体   繁体   中英

Send a Post request with the current excel sheet as multi-part form data using vba

I'm trying to send the current working excel sheet to an api accepting Content-type: "multipart-form". I need help to form the request in vba. Following is my approach:

Sub sendInternalDataToAPI(myCSVFileName As String)
    Dim data As Worksheet
    Dim boundary As String
    Dim filename As String


    boundary = "--------------------------784780577729000449617522"
    filename = "data"
    Set data = ActiveWorkbook.Sheets("DATA")
    'Set payload = preparePayload(data)
    Url = "http://localhost:8080/load"
    Set objHTTP = CreateObject("Microsoft.XMLHTTP")

    With objHTTP
    .Open "POST", Url, False
    .setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
    .setRequestHeader "Content-type", "multipart/form-data;boundary=" & boundary
    .send (preparePayload(data, boundary, myCSVFileName))
    strResponseStatus = .StatusText
    strResponse = .ResponseText
    allResponseHeader = .GetAllResponseHeaders
    End With
    Debug.Print strResponseStatus
    Debug.Print allResponseHeader
    Debug.Print strResponse
End Sub

Function preparePayload(data As Worksheet, boundary As String, filename As String) As String

    Debug.Print boundary & vbCrLf & _
    "Content-Disposition: form-data; name=""data""; filename=""" & filename & """" & vbCrLf & _
    "Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" & vbCrLf & _
    data & vbCrLf & _
    boundary & "--"

    preparePayload = boundary & vbCrLf & _
    "Content-Disposition: form-data; name=""data""; filename=""" & filename & """" & vbCrLf & _
    "Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" & vbCrLf & _
     data & vbCrLf & _
     boundary & "--"
End Function

On Executing I get the following error:

VBA Error: 438: Object doesn't support this property or method

The variable "data" in the form body is causing the error. I'm not sure what am I missing! And, I got the Content-type in request-body from postman, which is executing fine! I'm quite new in vba, any help is deeply appreciated.

Thanks!

UPDATE: The postman request from the postman console is:

Request ---
POST <url>/load HTTP/1.1
User-Agent: PostmanRuntime/7.22.0
Accept: */*
Cache-Control: no-cache
Postman-Token: d3a4355f-548a-4b10-b8b7-53c62772cc4a
Host: localhost:8080
Content-Type: multipart/form-data; boundary=--------------------------360126624207643168890089
Accept-Encoding: gzip, deflate, br
Content-Length: 352982
Connection: keep-alive
----------------------------360126624207643168890089
Content-Disposition: form-data; name="data"; filename="test-excel.xlsx"

<test-excel.xlsx>
----------------------------360126624207643168890089--


Response---

HTTP/1.1 200 OK
Date: Tue, 03 Mar 2020 13:41:17 GMT
Content-Type: application/json
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: 0
X-Frame-Options: DENY
Transfer-Encoding: chunked

response Body fromt the server--
{"data":true}

How is this working with only the file name in the request body and not the contents??

I believe the problem is that you're trying to "convert" the Worksheet variable "data" into a String in your preparePayload function .

I'm not sure what you need to "prepare" for the API you're using, if it's the name of the Worksheet itself you can get it by using the .Name method, if it's something else you need to make sure to properly convert it to a String, or make sure it can be implicitly converted.

Finally, got it done!

As @TimWilliams mention in the comments, basically I had to: 1. save the file first from excel 2. read it back to excel as String 3. Convert to Bytes and then send it via http

Also this post helped dearly: google.com/search?q=vba+post+excel+file+site:stackoverflow.com

Also, if there was a way to send the ActiveWorkbook.sheet("MY_SHEET") directly to the api, that would have been really nice. Will keep exploring!

Thanks @TimWilliams!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM