We help IT Professionals succeed at work.

VBScript - How to POST an XML file to a URL

Lico_w
Lico_w asked
on
I have an XML file which gets created periodically and need a simple VBScript function to POST the file to a URL, can anyone assist please?
Comment
Watch Question

Project Lead
Top Expert 2011
Commented:
You can do something like this

Function HTTPPost(sUrl, sRequest)
  set oHTTP = CreateObject("Microsoft.XMLHTTP")
  oHTTP.open "POST", sUrl,false
  oHTTP.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
  oHTTP.setRequestHeader "Content-Length", Len(sRequest)
  oHTTP.send sRequest
  HTTPPost = oHTTP.responseText
End Function

HTTPPost("your_ulr", "your xml")

Author

Commented:
Thanks for that. So if my xml was stored locally on my C:\ presumably I'd just create a variable with that absolute location and pass it in the method call as sRequest?

Also what can I do to see the http response code?
Kiran SonawaneProject Lead
Top Expert 2011

Commented:
1) No. If the xml is in file then you need to load the xml from file like

Set oXML = CreateObject("Microsoft.XMLDOM")    
 oXML.async = False    
 oXML.load("C:\test.xml")    
 
 oHTTP.send(oXML.xml)  

2) You can get the response like below

If(oHTTP.readyState = 4) then  
 
 XMLResponse = oHTTP.responseText  
 
 Msgbox XMLResponse
 

Author

Commented:
Ok think I've got it! So my function looks like this now, can you confirm if this is correct and I'll close the case:

 Function HTTPPost(sUrl, sRequest)
  Set oXML = CreateObject("Microsoft.XMLDOM")    
  oXML.async = False    
  oXML.load(sRequest)
  set oHTTP = CreateObject("Microsoft.XMLHTTP")
  oHTTP.open "POST", sUrl, false
  oHTTP.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
  oHTTP.setRequestHeader "Content-Length", Len(sRequest)
  oHTTP.send oXML
  If(oHTTP.readyState = 4) then  
  XMLResponse = oHTTP.responseText  
  Msgbox XMLResponse
End Function
Kiran SonawaneProject Lead
Top Expert 2011

Commented:
1) It looks like you are missing End If of If statement
2) Yes. sRequest contains path to your xml file
Kiran SonawaneProject Lead
Top Expert 2011

Commented:
Also you might need to set character set to content type like below (Not sure)
oHTTP.setRequestHeader(”Content-type”, “application/x-www-form-urlencoded; charset=UTF-8¿);

Author

Commented:
I can't get this to work, my code is below, it keeps failing on the send part but the error is unfriendly.

 Function HTTPPost(sUrl, sRequest)
  'create an xml object
  Set oXML = CreateObject("Microsoft.XMLDOM")
  oXML.async = False
  'load the xml file
  oXML.load(sRequest)
 
  set oHTTP = CreateObject("Microsoft.XMLHTTP")
  oHTTP.open "POST", sUrl, false
  'oHTTP.setRequestHeader(”Content-type”, “application/x-www-form-urlencoded; charset=UTF-8");
  oHTTP.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
  oHTTP.setRequestHeader "Content-Length", Len(sRequest)
  oHTTP.send oXML
  'If(oHTTP.readyState = 4) then  
      'XMLResponse = oHTTP.responseText  
      'Msgbox XMLResponse
  'End if
End Function

result = HTTPPost("https://someURL", "C:\tnt_plane.xml")
Msgbox result
Aurelian ConstantinProgrammer-analyst

Commented:
Before the End Function statement, you have to add this line:

HTTPPost = oHTTP.responseText

Author

Commented:
.