1. Daniel Ferreira
  2. PowerBuilder
  3. Monday, 28 January 2019 23:02 PM UTC

Hi guru guys,

I have been successful  calling a REST API using OLE Objets Msxml2.DOMDocument.6.0 and MSXML2.ServerXMLHTTP.6.0.

Now, I have an API with "Request Data" that I'm not being able to call from PB. Can any of you help?

This is the sample that works, it has no "Request Data":
ID = 97E0A5A956B64A74A4C241F6FFF19A82
api_token = d0f746328e2087a04a691879b803178d (yes, is for test purposes)
url = https://api.iugu.com/v1/subscriptions/ID/change_plan/teste

This is my code:
OleObject lole_Send
OleObject lole_SrvHTTP

String ls_status, ls_response_json
String ls_url = "https://api.iugu.com/v1/subscriptions/97E0A5A956B64A74A4C241F6FFF19A82/change_plan/teste?api_token=d0f746328e2087a04a691879b803178d"

lole_Send = Create OleObject
lole_SrvHTTP = Create OleObject
lole_Send.connectToNewObject("Msxml2.DOMDocument.6.0")
lole_SrvHTTP.connectToNewObject("MSXML2.ServerXMLHTTP.6.0")
lole_SrvHTTP.Open('POST', ls_url, False)
lole_SrvHTTP.SetRequestHeader("Content-Type", "application/json")
lole_SrvHTTP.Send(lole_Send)

ls_status = String(lole_SrvHTTP.Status)
ls_response_json = String(lole_SrvHTTP.ResponseText)

Now, this new API that I have to call has something called "REQUEST DATA", and I'm being able to send it. Can you advise? This is the metadata and JavaScript sample to it:

Metadata
METHOD: PUT
URL: https://api.iugu.com/v1/subscriptions/3EF8E2BB6DE44DD1A6D6DDF6492DB5C7?api_token=d0f746328e2087a04a691879b803178d
REQUEST HEADERS:
Accept: application/json
Content-Type: application/json
REQUEST DATA: (this is what I'm not being able to send)
{"plan_identifier":"teste","skip_charge":true}

Javascript sample:

var data = JSON.stringify({
  "plan_identifier": "teste",
  "skip_charge": "true"
});

var xhr = new XMLHttpRequest();
xhr.withCredentials = true;

xhr.addEventListener("readystatechange", function () {
  if (this.readyState === this.DONE) {
    console.log(this.responseText);
  }
});

xhr.open("PUT", "https://api.iugu.com/v1/subscriptions/3EF8E2BB6DE44DD1A6D6DDF6492DB5C7");

xhr.send(data);

Thanks, any help is appreciated.

Daniel

 

Who is viewing this page
Accepted Answer
Roland Smith Accepted Answer Pending Moderation
  1. Tuesday, 29 January 2019 02:41 AM UTC
  2. PowerBuilder
  3. # Permalink

Why are you sending an OLEObject? You should be sending a string containing the request data which is in JSON format. Usually the DOM COM object is used to work with the response you get from the call.

I have an example of using the XMLHTTP COM control:

http://www.topwizprogramming.com/freecode_xmlhttp.html

I have an example of using the WinHTTP API functions directly.

http://www.topwizprogramming.com/freecode_winhttp.html

 

Comment
There are no comments made yet.
Daniel Ferreira Accepted Answer Pending Moderation
  1. Tuesday, 29 January 2019 11:46 AM UTC
  2. PowerBuilder
  3. # 1

Thanks Roland, that's it.

I was using the same method as GET and POST, now I've separated them.

I'll send the JSON data on the lole_SrvHTTP.Send("my JSON data")

Thanks a lot.

Comment
There are no comments made yet.
Armeen Mazda @Appeon Accepted Answer Pending Moderation
  1. Monday, 28 January 2019 23:19 PM UTC
  2. PowerBuilder
  3. # 2

Why are you trying to use Msxml2.DOMDocument.6.0 to do this instead of the HTTPClient object?  Is it because you are not using PowerBuilder 2017?

Comment
  1. Reyzin Quero
  2. Wednesday, 30 January 2019 04:46 AM UTC
That's help me a lot. Thanks Armeen.
  1. Helpful
  1. Kevin Ridley
  2. Thursday, 31 January 2019 16:52 PM UTC
Reyzin,

Just fyi, if the resultset returned is reasonably simple and well defined, you can also use the RESTClient and have the results populated directly to a datawindow. Another option that I like is the OAuthClient. It works almost exactly the same as the HTTPClient, but allows you to use tokens (optional), and in my opinion is a little easier to use.
  1. Helpful
  1. Armeen Mazda @Appeon
  2. Thursday, 31 January 2019 20:21 PM UTC
Adding to what Kevin said... the RESTClient has been enhanced in PB 2019 so that it does not only retrieve directly into DataWindows but also supports full CRUD functionality and can handle both DataWindow-style JSON or just plain JSON and the JSON data can even be compressed (gzip).
  1. Helpful
There are no comments made yet.
  • Page :
  • 1


There are no replies made for this question yet.
However, you are not allowed to reply to this question.