1. Yasir Masood
  2. PowerBuilder
  3. Tuesday, 12 September 2023 19:12 PM UTC

Hi,

I am trying to use an API created in C# in my PB application. I am using PB Version 2017 R3 Build 1878.

To use the API, I need an Access Token that I am able to obtain using OAuthClient, TokenRequest and TokenResponse.

But I am not able to access the url with JSON.I get a return code of "415" and a response of "Unsupported Media Type".

Do I need get the latest version of PB to use this functionality?

Here is my code,

 

String ls_AccessToken
Long ll_return

long ll_ResponseStatusCode
string ls_ResponseStatusText
httpclient    http
string    ls_json    
int li
string ls_res


ls_AccessToken = "Bearer " + ls_AccessToken

ls_url = "https://google.com"
ls_json = "{'carInit':'GATX','carNo':'000312'}"

http = create httpclient
ll_Return = http.ClearRequestHeaders ( )
http.AutoReadData = false

ll_Return = http.setrequestheader("Authorization" , ls_AccessToken)

ll_Return=0
ll_Return = http.sendrequest( 'POST', ls_url, ls_json)

li=http.GetResponseStatusCode()
ls_res = http.GetResponseStatusText()

Thank you for your help

Accepted Answer
Daryl Foster Accepted Answer Pending Moderation
  1. Wednesday, 13 September 2023 02:01 AM UTC
  2. PowerBuilder
  3. # Permalink

You might need to add a content-type header:

 

http.SetRequestHeader('Content-Type', 'application/json')

 

If you are having trouble implementing it in Powerbuilder you should use something like Postman to manually call the API and then replicate the settings from Postman in Powerbuilder.

 

 

 

Comment
There are no comments made yet.
Yasir Masood Accepted Answer Pending Moderation
  1. Wednesday, 13 September 2023 22:26 PM UTC
  2. PowerBuilder
  3. # 1

After modifying the the json and adding the header for Content-Type, I am able to connect to the website.

I am getting a response code of "200" and response text of "OK". However, I am still not getting a result back

using http.GetResponseBody (ls_ReturnJson ). The return is -1 and the string is empty.

I would appreciate any help. Thanks


String ls_AccessToken
String ls_Url
String    ls_json

httpclient    http
Long ll_return

Long ll_ResponseStatusCode
String ls_ResponseStatusText    

String    ls_ReturnJson

ls_AccessToken = "Bearer " + ls_AccessToken

ls_url = "https://google.com"
ls_json = '{"carInit":"GATX","carNo":"000312"}'

http = create httpclient
ll_return = http.ClearRequestHeaders ( )
http.AutoReadData = false

ll_return=0
ll_return = http.setrequestheader("Authorization" , ls_AccessToken)
//ll_return = http.setrequestheader("Grant" , ls_AccessToken)
ll_return=0
ll_return = http.SetRequestHeader("Content-Type", "application/json;charset=UTF-8")

ll_return=0
ll_return = http.sendrequest( 'POST', ls_url, ls_json, EncodingUTF8!)

ll_ResponseStatusCode=http.GetResponseStatusCode()
ls_ResponseStatusText = http.GetResponseStatusText()

IF ll_ResponseStatusCode = 200 THEN
    ll_return=0
    ll_return = http.GetResponseBody (ls_ReturnJson )
    
END IF

Comment
  1. Daryl Foster
  2. Wednesday, 13 September 2023 23:48 PM UTC
Comment out this line:



http.AutoReadData = false



and see if that helps. I've never set that property to false. If it's false I'm pretty sure you need to use ReadData which you only need to do if the response is going to be really large. If you comment out the line it will default to true and you should be able to read the response with GetResponseBody
  1. Helpful 2
  1. Yasir Masood
  2. Thursday, 14 September 2023 01:06 AM UTC
This was the issue. Thank you Daryl and everyone else for your help
  1. Helpful
  1. Daryl Foster
  2. Thursday, 14 September 2023 01:42 AM UTC
No worries Yasir, glad I could help.
  1. Helpful
There are no comments made yet.
Yasir Masood Accepted Answer Pending Moderation
  1. Tuesday, 12 September 2023 19:36 PM UTC
  2. PowerBuilder
  3. # 2

If I use ll_Return = http.setrequestheader("Grant" , ls_AccessToken),

I get a return of "401" and response "Unauthorized"

 

Comment
There are no comments made yet.
Kevin Ridley Accepted Answer Pending Moderation
  1. Tuesday, 12 September 2023 19:30 PM UTC
  2. PowerBuilder
  3. # 3

I'm pretty sure in PB 2017 you need to use the Password Grant, not Authorization.

Comment
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.