1. VISHALAKSHI RAO
  2. PowerBuilder
  3. Thursday, 14 July 2022 17:20 PM UTC

Hi,

How do we calculate "Content-Length" while consuming a POST REST call in PowerBuilder 2019 version? I have used LEN function to check the length of the body. But its not working. I have attached PB code for it. Could you please suggest a solution to this?

 

Regards,

Vishalakshi

ls_email = "abc@gmail.com"
ls_password = "1234"
ls_body = "email= " + ls_email + "&" + "password= " + ls_password
ls_url = "https://XXX/login/authenticate"

lnv_RestClient = CREATE RestClient
lnv_package = CREATE JsonPackage

ls_content_length = String(Len(ls_body))  //This is not giving the required data

lnv_RestClient.SetRequestHeader( "Content-Type","application/x-www-form-urlencoded")
lnv_RestClient.SetRequestHeader( "Host", "maven.gscmaven.com")
lnv_RestClient.SetRequestHeader( "Content-Length", ls_content_length)
	
li_rc = lnv_RestClient.SendPostRequest(ls_url, ls_body, ls_response)
Accepted Answer
Arnd Schmidt Accepted Answer Pending Moderation
  1. Thursday, 14 July 2022 22:39 PM UTC
  2. PowerBuilder
  3. # Permalink

How about removing the space after the '=' sign  to send proper key=value pairs?


ls_body = "email=" + ls_email + "&" + "password=" + ls_password

Also Keep in mind that for special characters UTF-8 uses a multibyte representation - so you have to set the length in bytes (aka octets) in the request header.

hth

Arnd

 

Comment
  1. VISHALAKSHI RAO
  2. Friday, 15 July 2022 00:08 AM UTC
HI Arnd,

Thank you. This solution worked! So, the problem was with Space, not on "Content-Length". Thank you very much for suggesting!
  1. Helpful 1
There are no comments made yet.
Miguel Leeuwe Accepted Answer Pending Moderation
  1. Thursday, 14 July 2022 18:15 PM UTC
  2. PowerBuilder
  3. # 1

Please describe "is not working".

Is the result of 

ls_content_length = String(Len(ls_body))

not working? 

Or is your subsequent call to 

lnv_RestClient.SetRequestHeader( "Content-Length", ls_content_length)
	
li_rc = lnv_RestClient.SendPostRequest(ls_url, ls_body, ls_response)

not working given the length in the setrequestheader ?

 

regards.

MiguelL

Comment
  1. VISHALAKSHI RAO
  2. Thursday, 14 July 2022 19:52 PM UTC
Hi MiguelL,

Thank you for your response.



For my REST call, "Content-Length" is mandatory input. if I don't include this or pass wrong value, I get "Unauthorized Access" error. I have verified in POSTMAN. I get same error in POSTMAN too, If I don't include this Header. If I include this header in POSTMAN, it works fine. But this header value is calculated automatically in POSTMAN.

So, this CONTENT-LENGTH header is not having the right data & hence unable to make the call



Regards,

Vishalakshi
  1. Helpful
There are no comments made yet.
Olan Knight Accepted Answer Pending Moderation
  1. Thursday, 14 July 2022 18:01 PM UTC
  2. PowerBuilder
  3. # 2

 

What is the result you are getting?

If your method is not working, try breaking it down into its components, like this:

ll_len = 0
ll_len = LEN (ls_body)

ls_content_length = STRING (ll_len)

Comment
  1. VISHALAKSHI RAO
  2. Thursday, 14 July 2022 19:54 PM UTC
Hi Olan,

Thank you for your reply.



I have tried breaking down. But not successful.



For my REST call, "Content-Length" is mandatory input. if I don't include this or pass wrong value, I get "Unauthorized Access" error. I have verified in POSTMAN. I get same error in POSTMAN too, If I don't include this Header. If I include this header in POSTMAN, it works fine. But this header value is calculated automatically in POSTMAN.

So, this CONTENT-LENGTH header is not having the right data & hence unable to make the call



Regards,

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