Page 1 of 3

API Call

Posted: Thu Oct 13, 2016 8:45 am
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

Re: API Call

Posted: Thu Oct 13, 2016 9:22 am
by rdonnay
It looks like they want a Javascript object (JSON) to be sent.

I would need more information.

Re: API Call

Posted: Thu Oct 13, 2016 1:30 pm
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

Re: API Call

Posted: Thu Oct 13, 2016 4:31 pm
by rdonnay
What is the URL for their API?

Re: API Call

Posted: Fri Oct 14, 2016 1:48 am
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, ....

Re: API Call

Posted: Fri Oct 14, 2016 6:13 am
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

Re: API Call

Posted: Sun Oct 16, 2016 1:58 am
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.

Re: API Call

Posted: Mon Oct 17, 2016 12:24 am
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 )

Re: API Call

Posted: Mon Oct 17, 2016 6:25 am
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

Re: API Call

Posted: Mon Oct 17, 2016 8:15 am
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.