API Call

This forum is for eXpress++ general support.
Message
Author
omni
Posts: 554
Joined: Thu Jan 28, 2010 9:34 am

API Call

#1 Post by omni »

Roger,

Been so long not sure best option to use. Been using SOAP for last 5+ years and a new vendor want to use an API call.

What is the best way with current available options, or is that not enough information.

They have a web test module with examples. The following that sends an email gets a response of '200' if successful, which it is on their web page.

curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' --header 'Authorization: Basic dGZsbzA2YzpMTGJCUnYyTFNkTnJUdkZlbG85M2VnPT0=' -d '
{
"Type": "notification",
"RecipientId": "OMNITEST",
"Driver": "jnavarro@transflo.com",
"NotificationTitle": "Urgent message",
"NotificationBody": "Attention driver, you need to call your manager ASAP. Thank you"
}' 'http://webapp.transflodev.com/svc1.tran ... ifications'


Fred
Omni

User avatar
rdonnay
Site Admin
Posts: 4813
Joined: Wed Jan 27, 2010 6:58 pm
Location: Boise, Idaho USA
Contact:

Re: API Call

#2 Post by rdonnay »

It looks like they want a Javascript object (JSON) to be sent.

I would need more information.
The eXpress train is coming - and it has more cars.

omni
Posts: 554
Joined: Thu Jan 28, 2010 9:34 am

Re: API Call

#3 Post by omni »

From what they says its a standard post.

Trying to use

nPort := INTERNET_DEFAULT_HTTP_PORT
STORE 'TRANSFLO.TXT' TO EXFILE

cHost :="http://webapp.transflodev.com/svc1.tran ... ifications"
cText := MemoRead( Exfile )

cResponse := LoadFromUrl( cHost, NPORT, , , , 'POST', cText )

The cResponse is just nothing. Should get something like invalid credentials...but its nil. Even if cText is blank I should get some sort of response, I thought.


Am I missing something?


Fred

User avatar
rdonnay
Site Admin
Posts: 4813
Joined: Wed Jan 27, 2010 6:58 pm
Location: Boise, Idaho USA
Contact:

Re: API Call

#4 Post by rdonnay »

What is the URL for their API?
The eXpress train is coming - and it has more cars.

skiman
Posts: 1199
Joined: Thu Jan 28, 2010 1:22 am
Location: Sijsele, Belgium
Contact:

Re: API Call

#5 Post by skiman »

Hi,

It looks as they have an API based on REST? Should work with loadfromurl(). You have to create a json according to the API. Maybe there is also some authentication needed.

There should be some documentation about the way you can connect, to form the json, how to authenticate, how to post, what the result of a request should be, ....
Best regards,

Chris.
www.aboservice.be

omni
Posts: 554
Joined: Thu Jan 28, 2010 9:34 am

Re: API Call

#6 Post by omni »

Yes, it is based on rest.

That is the real url on my above code. The file is their file they gave me from their web sample, which includes BASIC authentication (a Long code) and two header records. When the results come in the code to get the proper results were also given, but it does not work when we use it.

Is there a method to watch the post?

Fred

c-tec
Posts: 379
Joined: Tue Apr 20, 2010 1:36 am
Location: SALZBURG/AUSTRIA
Contact:

Re: API Call

#7 Post by c-tec »

Hello,
I do all this stuff with Chilkat, maybe this litte samples can help.
regards
Rudolf


Code: Select all

function jsontest()
******************************************************************
altd()
loJson := CreateObject('Chilkat_9_5_0.JsonObject')
lcJsonStr := '{ "id": 1, "name": "A green door", "tags": ["home", 22, "green"], "price": 125 }'

lnSuccess := loJson:Load(lcJsonStr)
IF (lnSuccess <> 1)
     cError := loJson:LastErrorText
     loJson:destroy()
else
     lnNumMembers := loJson:Size
     FOR i := 1 TO lnNumMembers
          lcName := loJson:NameAt(i)
          lcValue := loJson:StringAt(i)
          dcqdebug lcName + ": " + lcValue
          lnIValue = loJson:IntAt(i)
          dcqdebug lcName + " as integer: " + STR(lnIValue)
          loJson:destroy()
     NEXT i
endif
return .t.


function pbupload()
******************************************************************
LOCAL loReq
LOCAL loHttp
LOCAL lnSuccess
LOCAL lcJsonText
LOCAL loResp
local cUrl  := "https://api.pushbullet.com/v2/pushes"

loReq := CreateObject('Chilkat_9_5_0.HttpRequest')
loHttp := CreateObject('Chilkat_9_5_0.Http')
altd()
*  Any string unlocks the component for the 1st 30 days.
lnSuccess := loHttp:UnlockComponent(cLicense)
IF (lnSuccess <> 1)
    dcqdebug  loHttp:LastErrorText
    loReq:destroy()
    loHttp:destroy()
    return .f.
ENDIF
loReq:AddParam("Access-Token",cToken)
loReq:AddParam("Content-Type",  "multipart/form-data; charset=UTF-8" )
loReq:AddParam("file-name",)
loReq:AddParam("file_type",)
loReq:AddParam("file_url",)

loHttp:AcceptCharset := ""
loHttp:UserAgent := ""
loHttp:AcceptLanguage := ""
loHttp:AllowGzip := 0
loResp := loHttp:PutBinary(cUrl,memoread("sample1.jpg"))

IF empty(loResp)
    dcqdebug  loHttp:LastErrorText
ELSE
    *  Display the JSON response.
    dcqdebug  loResp:BodyStr
    loResp:destroy()
ENDIF

loReq:destroy()
loHttp:destroy()

return .t.
Rudolf Reinthaler
digital pen & paper systems
http://www.formcommander.net

skiman
Posts: 1199
Joined: Thu Jan 28, 2010 1:22 am
Location: Sijsele, Belgium
Contact:

Re: API Call

#8 Post by skiman »

Hi,

You also need to send the header.

Code: Select all

cHeader := 'Content-Type: application/json' + CRLF
cHeader += 'Accept: application/json' + CRLF
cHeader += 'Authorization: Basic dGZsbzA2YzpMTGJCUnYyTFNkTnJUdkZlbG85M2VnPT0='

LoadFromUrl( cHost, NPORT, , , , 'POST', cText, cHeader )
Best regards,

Chris.
www.aboservice.be

omni
Posts: 554
Joined: Thu Jan 28, 2010 9:34 am

Re: API Call

#9 Post by omni »

Hey,

How did you know their specs, including the BASIC? Is that just standard? This API type is new to me, although we have used many prior to using SOAP that had better examples from the web service. This one uses CURL for its examples, which I do not want to use.

What info did you send as the text? The only thing we have in there now is the script, in our case..

{
"Type": "notification",
"RecipientId": "OMNITEST",
"Driver": "fhh@xxxxx.com",
"NotificationTitle": "Urgent message",
"NotificationBody": "Attention Testing, this is a test from testomni. Thank you"
}

This is their most simple type normally used for initial testing the set up.

Thanks

Fred

skiman
Posts: 1199
Joined: Thu Jan 28, 2010 1:22 am
Location: Sijsele, Belgium
Contact:

Re: API Call

#10 Post by skiman »

Hi,

The header comes from the url you posted.
curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' --header 'Authorization: Basic dGZsbzA2YzpMTGJCUnYyTFNkTnJUdkZlbG85M2VnPT0=' -d '
cText must contain your json string as you already posted.
Best regards,

Chris.
www.aboservice.be

Post Reply