1. Winthor Caingles
  2. PowerBuilder
  3. Monday, 6 February 2023 12:56 PM UTC

 

Hello Appeon Support,

 

Good day!!!

 

I need your help, I currently working on a SMS blast project... I am using the HTTPClient and successfully retrieve the token, however,  I cannot send data using POST method to send SMS to the server...

 

below is the code I am using, assumption is token is already send and store to a variable..

 

CODE----

String ls_Basic, ls_UserName, ls_Password, ls_Body,ls_json, ls_string
Integer li_Return
CoderObject lco_Code
HttpClient lhc_Client

lco_Code = Create CoderObject
lhc_Client = Create HttpClient

dw_2.AcceptText()
ls_json = dw_2.exportjson()

ls_Basic = lco_code.base64encode( Blob(ls_token , EncodingUTF8!))

lhc_Client.SetRequestHeader( "Authorization", "Basic " + ls_basic)
lhc_Client.SetRequestHeader("Content-Type", "application/json;charset=UTF-8")

li_Return = lhc_Client.SendRequest("POST", "https://messagingsuite.smart.com.ph/cgpapi/messages/sms",ls_json)

mle_1.text = lhc_Client.GetResponseStatusText()
If li_Return = 1 And lhc_Client.GetResponseStatusCode() = 200 Then
lhc_Client.GetResponseBody(ls_string)
Else
messagebox("error","error")

End If

 

END---

 

DOCUMENTATION FROM THE PROVIDER

Example Connect API Requests

SMS Request

POST https://messagingsuite.smart.com.ph/cgpapi/messages/sms

Authorization: Basic abcdef1234567890==

Content-Type: application/json;charset=UTF-8

Content-Length: 358 

{
"messageType":"sms", "destination":"61299002200", "text":"Hello Connect API"

}

 

 

Appreciate your help...

thank you very much....

Accepted Answer
Daryl Foster Accepted Answer Pending Moderation
  1. Tuesday, 7 February 2023 03:43 AM UTC
  2. PowerBuilder
  3. # Permalink

Hi Winthor, do you have Postman, or know how to use it?  That is usually the easiest way to trouble shoot these problems. If you can manually get something to work in Postman it is usually pretty easy to replicate that in Powerbuilder.

If you are using a token from an api you don't need to convert it, you would just use it directly as a Bearer token.  The documentation you show from the provider does say to use Basic authentication, which does require a username.  I can see that you've also declared a ls_UserName variable, is that the username you use to retrieve your token?

You could try and use the username and the token to see if it works with basic auth. You could try this and see if it works, but if it doesn't I'd highly recommend using Postman and getting it to work from there.

ls_Basic = lco_code.base64encode( Blob(ls_UserName + ":" + ls_token , EncodingUTF8!))

lhc_Client.SetRequestHeader( "Authorization", "Basic " + ls_basic)

 

Comment
There are no comments made yet.
Daryl Foster Accepted Answer Pending Moderation
  1. Monday, 6 February 2023 23:52 PM UTC
  2. PowerBuilder
  3. # 1

Have you tried to use the token and json in Postman to make sure it works ok?

Is ls_token just the api token, or does it include a username?  Basic Auth uses a base64 encoded string in the form USERNAME:PASSWORD.  The username could be a token so the string would need to be base64 encoded USERNAME:TOKEN, not just base64 encoded TOKEN.

Usually if you are just passing a token you would use Bearer rather than Basic, so you would just send 'Bearer ' + ls_token

Comment
  1. Winthor Caingles
  2. Tuesday, 7 February 2023 03:10 AM UTC
Hi Daryl,



The token is coming from the API server, I parsed it to get the access token part... then I put it on a string ls_token... Do I need to convert the ls_token? then I tried to use the Client.SetRequestHeader( "Authorization", "Bearer "+ls_Token )... it still not working... The Authorization part... do I need to put the username and passpword?
  1. Helpful
  1. Winthor Caingles
  2. Tuesday, 7 February 2023 03:21 AM UTC
The Token has not username...
  1. Helpful
  1. Daryl Foster
  2. Tuesday, 7 February 2023 03:48 AM UTC
Hi Winthor, I replied above in a new reply.



Also, what is the exact error message you are getting back from the api? What is the ResponseStatusCode, the ResponseStatusText and the ResponseBody? You should be able to get the ResponseBody even if the ResponseStatusCode is not 200 so you'll need to modify your code a bit. Those 3 things are all important to work out what is failing.



Also, have you checked your ls_json to make sure it is valid?
  1. Helpful
There are no comments made yet.
Roland Smith Accepted Answer Pending Moderation
  1. Monday, 6 February 2023 15:16 PM UTC
  2. PowerBuilder
  3. # 2

SendRequest has an undocumented length limit which is fairly short.

You should use PostDataStart/PostData/PostDataEnd instead.

Comment
  1. Winthor Caingles
  2. Tuesday, 7 February 2023 03:13 AM UTC
Hi Roland,



What do you mean by this? sorry I am new to this...

thanks
  1. Helpful
  1. Roland Smith
  2. Tuesday, 7 February 2023 13:37 PM UTC
Find HttpClient in the help file and click on PostDataStart in the list of functions. There is an example there.
  1. Helpful
There are no comments made yet.
Andreas Mykonios Accepted Answer Pending Moderation
  1. Monday, 6 February 2023 14:40 PM UTC
  2. PowerBuilder
  3. # 3

Hi.

Some services require to send content length information. It exists in the example... Are you sure your issue isn't related to lack of this information?

Andreas.

Comment
  1. Winthor Caingles
  2. Tuesday, 7 February 2023 03:12 AM UTC
Hi Andreas,



thank you for the reply... What do you mean by content length?
  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.
We use cookies which are necessary for the proper functioning of our websites. We also use cookies to analyze our traffic, improve your experience and provide social media features. If you continue to use this site, you consent to our use of cookies.