Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

REST API VBA coding

Status
Not open for further replies.

Bilberry

Programmer
Dec 17, 2007
111
NL
Hi all,
I have the following code as example:
Code:
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;

public class TestLogin1
{

   public static void main(String[] args)
   {
       HttpClient httpClient = new HttpClient();
       PostMethod httpPost = null;
        
       try
       { 
           httpPost = new PostMethod("[URL unfurl="true"]https://na.mydomain.com/networking/rest/login");[/URL]
            
           String msg = "<?xml version=\"1.0\"?><mydomain ver=\"2.0\">";
           msg += "<login_request><login>youraccount@mydomain</login>";
           msg += "<password>your_password</password>";
           msg += "</login_request></mydomain>";
           
           httpPost.addParameter("xml_data", msg);
           httpClient.executeMethod(httpPost);
           String loginResponse = new String(httpPost.getResponseBody());
           System.out.println("Login Response \n" + loginResponse);
       }
       catch (Exception e)
       {
           e.printStackTrace();
       }
       finally
       {
           httpPost.releaseConnection();
           httpClient = null;
       }
   }
}

I have tried to write this code into VBA, but without success(i cannot login), any idea what im doing wrong:
THe code above is just the example of the vendor...
Code:
Public Sub main()
    Set objHTTP = CreateObject("MSXML2.XMLHTTP")
Dim myxml As String

myxml = "<?xml version=""1.0""?>" & _
            "<mydomain ver=""2.0"">" & _
            "<login_request>" & _
            "<login>youraccount@mydomain</login>" & _
            "<password>your_password</password>" & _
            "</login_request>" & _
            "</mydomain>"
            

objHTTP.Open "POST", "[URL unfurl="true"]https://na.mydomain.com/networking/rest/login",[/URL] False, myxml
objHTTP.setRequestHeader "Content-Type", "application/x-[URL unfurl="true"]www-form-urlencoded"[/URL]
objHTTP.send 
result = objHTTP.responseText   ' Get the result


MsgBox (result)


End Sub
 
you may need to add it to your references in VBE - Tools>References

Rgds, Geoff

We could learn a lot from crayons. Some are sharp, some are pretty and some are dull. Some have weird names and all are different colours but they all live in the same box.

Please read FAQ222-2244 before you ask a question
 
Thanks XLBO for the reply, Im working within Excel 2003 (VBA), this option isn't available. The following message is shown when running the code:

HTTP Status 405 -

--------------------------------------------------------------------------------

type Status report

message

description The specified HTTP method is not allowed for the requested resource ().


--------------------------------------------------------------------------------

Apache Tomcat/6.0-snapshot
 


Im working within Excel 2003 (VBA), this option isn't available.
NOT TRUE!!! In the VB Editor Tools > References...

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Ok, sorry -:) it is available, but which option to select?
 


You will have to scroll to look for an HTTP library. You may need to experiment.

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Hi Skip,
Could you please give an example of the HTTP library. Couldnt find a library with HTTP within the discription...
 




Microsoft WinHTTP Services

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Hi Skip,
I have done that, but i think i can communicate through REST. but i can not login, do you know what changes i need to make in my code, i have really tried everything (POST/GET) application/xml and other format, but without succes, this is the code and at the bottom the answer from the webservice:

Code:
Public Sub main()
Set objHTTP = CreateObject("MSXML2.XMLHTTP")
Dim myxml As String

            
myxml2 = "<platform>" & _
          "<login>" & _
            "<userName>username@domain</userName>" & _
            "<password>password</password>" & _
          "</login>" & _
          "</platform>"
            

objHTTP.Open "POST", "[URL unfurl="true"]https://na.mydomain.com/networking/rest/login",[/URL] False, myxml2
objHTTP.setRequestHeader "Content-Type", "application/xml"
objHTTP.send
result = objHTTP.responseText   

Range("a1") = (result)
End Sub

And this is the response:

Code:
<html><head><title>Apache Tomcat/6.0-snapshot - Error report</title><style><!--H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}HR {color : #525D76;}--></style> </head><body><h1>HTTP Status 400 - </h1><HR size="1" noshade="noshade"><p><b>type</b> Status report</p><p><b>message</b> <u></u></p><p><b>description</b> <u>The request sent by the client was syntactically incorrect ().</u></p><HR size="1" noshade="noshade"><h3>Apache Tomcat/6.0-snapshot</h3></body></html>
 
It works sorry. I have put the xml content behind the objHTTP.send, strange but it works! Thanks skip for your support
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top