-1

I'm trying to upload a tiny text file in a website using vba. When I run the script I encounter this error {"success":false,"error":400,"message":"Trouble uploading file"}. I mimicked the same approach using vba that I did and found success using python. I got rid of the headers altogether in python so I suppose multipart headers is not that important to upload the file successfully.

Using vba (I got the above error):

Sub UploadFile()
    Dim Http As New XMLHTTP60, sPostData$
    Dim nFile&, baBuffer() As Byte

    nFile = FreeFile
    Open "C:\Users\WCS\Desktop\some_text.txt" For Binary Access Read As nFile
        If LOF(nFile) > 0 Then
            ReDim baBuffer(0 To LOF(nFile) - 1) As Byte
            Get nFile, , baBuffer
            sPostData = StrConv(baBuffer, vbUnicode)
        End If
    Close nFile

'    MsgBox sPostData 'to examine if it is able to print the text

    With Http
        .Open "POST", "https://file.io/"
        .setRequestHeader "x-requested-with", "XMLHttpRequest"
        .send ("file=" & sPostData)

        Debug.Print .responseText
    End With
End Sub

Using vba (another way but got the same error):

Sub UploadFile()
    Dim Http As New XMLHTTP60, sPostData$

    With CreateObject("ADODB.Stream")
        .Charset = "utf-8"
        .Open
        .LoadFromFile ("C:\Users\WCS\Desktop\some_text.txt")
        sPostData = .ReadText()
    End With

    With Http
        .Open "POST", "https://file.io/"
        .setRequestHeader "x-requested-with", "XMLHttpRequest"
        .send ("file=" & sPostData)

        Debug.Print .responseText
    End With
End Sub

Using python (I got success):

import requests

url = 'https://file.io/'

files = {
   'file': open('some_text.txt','rb') 
}

def upload_file(link):
    res = requests.post(link,files=files)
    print(res.content)

if __name__ == '__main__':
    upload_file(url)

Btw, this is what the text file contains hi there!!!

asmitu
  • 175
  • 11
  • Have a look at this [LINK](https://stackoverflow.com/questions/60890492/) – YasserKhalil Mar 31 '20 at 10:13
  • 1
    Yep, I noticed that in the first place. The thing is I got success without using `multipart` headers in python, so I'm trying to figure out why it fails if I go for vba. I tried with `ServerXMLHTTP60` (supposed to handle cookies) as well but no luck. – asmitu Mar 31 '20 at 10:17
  • @asmitu where's the stream object which is important to be set ? – αԋɱҽԃ αмєяιcαη Mar 31 '20 at 10:24

1 Answers1

2

If the file was textfile, you can store the contents in a variable and in this case to send the contents as text

Sub UploadFile()
Dim http As New XMLHTTP60, sPostData$

With CreateObject("ADODB.Stream")
    .Charset = "UTF-8"
    .Open
    .LoadFromFile (ThisWorkbook.Path & "\Sample.txt")
    sPostData = .ReadText()
End With

With http
    .Open "POST", "https://file.io"
    .setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
    .send ("text=" & sPostData)
    Debug.Print .responseText
End With
End Sub

The code depends on the code derived from this LINK

YasserKhalil
  • 9,138
  • 7
  • 36
  • 95
  • 1
    So it's count as a duplicated question ! we aren't supposed to answer a `dupe` question at all. – αԋɱҽԃ αмєяιcαη Mar 31 '20 at 10:28
  • Maybe the same question but different approaches to solving the same issue .. – YasserKhalil Mar 31 '20 at 10:32
  • 1
    So, `Content-Type` headers is a must, right? Thanks for your solution @YasserKhalil. – asmitu Mar 31 '20 at 10:34
  • @asmitu before asking a question, make some little effort by searching in the community. that's will save both times for us. – αԋɱҽԃ αмєяιcαη Mar 31 '20 at 10:35
  • 1
    My several attempts above are the proofs that i gave a huge effort and tried myself to solve the issue before posting here a question @αԋɱҽԃ αмєяιcαη. – asmitu Mar 31 '20 at 10:51
  • Hi @YasserKhalil, why you used `text` instead of `file` in this particular line `.send ("text=" & sPostData)`? When I record the xhr in chrome dev tools, I could see `file: (binary)` in there which is why it confuses me. Thanks. – SIM Apr 02 '20 at 17:13
  • @SIM Using `file: (binary)` will require a lot of preparations in the code while using `text` is much simpler. – YasserKhalil Apr 02 '20 at 17:32