title: VBA XMLHTTP POST Upload text/xml File to API Service
body: <p>I need to send XML file to API service with use of Excel/VBA.</p>
  <p>Instructions available in documentation state that the only required field is:</p>
  <p><strong>file: string($binary)</strong> - file to upload. The name can be specified in the filename parameter of the Content-Disposition header.</p>
  <p>This is the equivalent of CURL based on documentation:</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>    curl -X 'POST' \
      'api_service_url' \
      -H 'accept: */*' \
      -H 'Authorization: Bearer XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX=' \
      -H 'Content-Type: multipart/form-data' \
      -F 'file=@VPRT9000004726.xml;type=text/xml'</code></pre>
  </div>
  </div>
  </p>
  <p>I have searched the web and cannot find a working solution. I am wondering, if this is something possible to do with Excel VBA XMLHTTP library at all?</p>
  <p>Available documentation of API service can be found on this link under the UPLOAD section:</p>
  <p><a href="https://testapi.valenciaportpcs.net/messaging/swagger/index.html" rel="nofollow noreferrer">https://testapi.valenciaportpcs.net/messaging/swagger/index.html</a></p>
  <p>Any help or direction appreciated.</p>
  <p>Code that is working for uploading the file, but having issues with UTF-8 special characters:</p>
  <pre><code>Option Explicit
  
  Sub UploadFile()
  
  Dim sFile As String
  Dim sUrl As String
  Dim sAccessToken As String
  Dim sBoundary As String
  Dim sResponse As String
  
      sFile = &quot;D:\VPRT9000004726.xml&quot;
      sUrl = &quot;https://testapi.valenciaportpcs.net/messaging/messages/upload/default&quot;
      sBoundary = &quot;---------------------------166096475834725259111917034354&quot;
      sAccessToken = &quot;myaccesstoken&quot;
      
      sResponse = pvPostFile(sUrl, sFile, sBoundary, sAccessToken)
      'Debug.Print sResponse
  
  End Sub
  
  Private Function pvPostFile(sUrl As String, sFileName As String, sBoundary As String, sAccessToken As String) As String
  
  Dim xmlReq As MSXML2.ServerXMLHTTP60
  Dim nFile As Integer
  Dim baBuffer() As Byte
  Dim sPostData As String
  
      '--- read file
      nFile = FreeFile
      Open sFileName For Binary Access Read As nFile
      If LOF(nFile) &gt; 0 Then
          ReDim baBuffer(0 To LOF(nFile) - 1) As Byte
          Get nFile, , baBuffer
          sPostData = StrConv(baBuffer, vbUnicode)
      End If
      Close nFile
      
      '--- prepare body
      sPostData = &quot;--&quot; &amp; sBoundary &amp; vbCrLf &amp; _
          &quot;Content-Disposition: form-data; name=&quot;&quot;file&quot;&quot;; filename=&quot;&quot;&quot; &amp; Mid$(sFileName, InStrRev(sFileName, &quot;\&quot;) + 1) &amp; &quot;&quot;&quot;&quot; &amp; vbCrLf &amp; _
          &quot;Content-Type: text/xml&quot; &amp; vbCrLf &amp; vbCrLf &amp; _
          sPostData &amp; vbCrLf &amp; _
          &quot;--&quot; &amp; sBoundary &amp; &quot;--&quot;
      'Debug.Print sPostData
        
      '--- post
      Set xmlReq = New MSXML2.ServerXMLHTTP60
      With xmlReq
          .Open &quot;POST&quot;, sUrl, False
          .setRequestHeader &quot;Authorization&quot;, &quot;Bearer &quot; &amp; sAccessToken
          .setRequestHeader &quot;Content-Type&quot;, &quot;multipart/form-data; boundary=&quot; &amp; sBoundary
          .setRequestHeader &quot;Accept-Charset&quot;, &quot;UTF-8&quot;
          .send pvToByteArray(sPostData)
          pvPostFile = .Status
      End With
      Set xmlReq = Nothing
      
  End Function
   
  Private Function pvToByteArray(sText As String) As Byte()
      pvToByteArray = StrConv(sText, vbFromUnicode)
  End Function
  </code></pre>