1. Malek Taha
  2. PowerBuilder
  3. Monday, 19 December 2022 17:21 PM UTC

Hi

   Can you please help me with httpclient API call. I am new to this and seems to be stuck.

I genetrated a JSON file using JSONGenerator object and the file is in the correct format.

I need to call an API that is expecting a JSON string.

How to pass the JSON string in my API call?

the call code is below

Please help

Thanks

Malek

 

 


HttpClient lhc_Client
lhc_Client = Create HttpClient

// Set request header
lhc_Client.SetRequestHeader("Accept", "*/*")
lhc_Client.SetRequestHeader("Accept-Encoding", "gzip")
lhc_Client.SetRequestHeader("Accept-Language", "en")
lhc_Client.SetRequestHeader("Connection", "keep-alive")
lhc_Client.SetRequestHeader("User-Agent", "Chrome/60.0.3112.113")
lhc_Client.SetRequestHeader("Cache-Control", "no-cache")
lhc_Client.SetRequestHeader( "Content-Type", "application/x-www-form-urlencoded" )

ls_key = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
lhc_Client.SetRequestHeader( "Authorization", " x-api-key" + ls_key)


li_Return = lhc_Client.SendRequest("POST", "https://shipper.qa.ptmx.io/loads")
If li_Return = 1 And lhc_Client.GetResponseStatusCode() = 200 Then
lhc_Client.GetResponseBody(ls_key)
MessageBox ("Hello" ,ls_key )
else
lhc_Client.GetResponseBody(ls_key)
MessageBox ("hello fail" ,ls_key )

End If
Destroy ( lhc_Client )

Daryl Foster Accepted Answer Pending Moderation
  1. Tuesday, 20 December 2022 00:59 AM UTC
  2. PowerBuilder
  3. # 1

To send json you need to add it to your request.  A simple example is

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

li_rc = lnv_HttpClient.SendRequest('POST', ls_url, as_json)

if li_rc = 1 then
    li_response_status = lnv_HttpClient.GetResponseStatusCode()
    ls_response_status_text = lnv_HttpClient.GetResponseStatusText()
    li_rc = lnv_HttpClient.GetResponseBody(ls_response_body)
else
    // Something went wrong
end if

 

A few other questions though

  1. You are using Content-Type "application/x-www-form-urlencoded", is the api expecting json or form data?
  2. In your example you're reading your response into the existing ls_key variable, is that deliberate?
  3. You've set your Authorization to " x-api-key" + ls_key, with a space before the x-api-key and no space afterwards, is that deliberate?

 

 

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.