VBA -- XMLHTTP POST method not working

Hi,

I'm trying to upload data to a web server for the first time.

The VBA function I'm using is below. This is for Excel 2007.

As it is now, I get the error code 500 ("Internal Service Error") when adding msgbox XmlHttp.Status at the end of the function.

The XmlText variable I'm using can be recreated using the attached file.

I'm very confident that the URL, UserName, and Password I'm using are correct (I've successfully used the GET method for a different objective with this web server). Unfortunately, I can't post those things here due to non-disclosure issues.

Straightforward documentation from the web server is available here:
https://www.powerclerkdemo.com/AppUpload/Schema/ApplicationUpload/ApplicationUpload.aspx

They say their service is exposed via a RESTful API. Maybe the function I'm using assumes SOAP. I don't know the difference as far as VBA is concerned.

Any suggestions greatly appreciated.

Regards,
Michael
Function PostXmlData(vURL As String, UserName As String, Password As String, XmlText As String) As String

Dim XMLHttp As Object


Set XMLHttp = CreateObject("MSXML2.XMLHTTP")


XMLHttp.Open "POST", vURL, False, UserName, Password

' Not sure which 'setRequestHeader' to use, if any.
'XMLHttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"   ' Status error 400 returned (Bad Request)
XMLHttp.setRequestHeader "Content-Type", "text/xml;charset=utf-8"   ' Status error 500 returned (Internal Service Error)

XMLHttp.send (XmlText)

PostXmlData = XMLHttp.responseText

End Function

Open in new window

XmlText-Code.xls
bishop3000Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

yogi4lifeCommented:
Hi,

Are you sure you can leave out the requestheader called content-length? It might not be necessary - but do check it...

    XMLHttp.setRequestHeader "Content-Length" , Str(Len(XmlText))


I can't see from your code that you are supplying a valid API-key either...
In a post it should be sent as a part of any request according to the links you provided...



bishop3000Author Commented:
Hi yogi4life,

Thanks a lot for your help.

The issue is that my xml text wasn't written correctly.
--I needed to add the following to my code (as the first line in the XML).
XmlText = "<?xml version=" & vQuote & "1.0" & vQuote & " " & "encoding=" & vQuote & "utf-8" & vQuote & "?>"

No, the content-length info isn't necessary, perhaps because it's REST.

Code that properly uploads XML text to the web is below.


Function XML_POST_PC(vURL As String, XmlText As String, UserName As String, Password As String) As String

Dim XMLHttp As Object

Set XMLHttp = CreateObject("MSXML2.XMLHTTP")    '  CreateObject("MSXML2.ServerXMLHTTP") -- a alternative with less freezing according to this post: http://www.bigresource.com/Tracker/Track-vb-50L5ormYPR/

XMLHttp.Open "POST", vURL, False, UserName, Password

XMLHttp.setRequestHeader "Content-Type", "text/xml"   ';charset=utf-8


XMLHttp.Send (XmlText)

XML_POST_PC = XMLHttp.ResponseText

End Function

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.