1. Sivaprakash BKR
  2. PowerBuilder
  3. Wednesday, 23 March 2022 10:40 AM UTC

Hello,

Need to PostData to a URL and read the result.   My code to PostData is and Read the response is

String   ls_email, ls_einvoice_json

ls_email = test@test.com
ls_einvoice_json = /*JSONVALUE*/     This JSON is verified to work

ls_url  = 'https://api.mastergst.com/einvoice/type/GENERATE/version/V1_03'
ls_url += '?email='

lb_url  = Blob(ls_email, EncodingAnsi!)
ls_url += lo_coder.URLEncode(lb_url)

lo_client = Create HttpClient
lo_client.SetRequestHeader("gstin",         '1')
lo_client.SetRequestHeader('username',      '2')
lo_client.SetRequestHeader('auth-token',    '3')
lo_client.SetRequestHeader('client_id',     '4')
lo_client.SetRequestHeader('client_secret', '5')
lo_client.SetRequestHeader('ip_address',    '6')
lo_client.SetRequestHeader("Content-Type", "multipart/form-data; boundary--------------------------359875084413580694217125")
lo_client.SetRequestHeader('Content-Length', String(Len(ls_einvoice_json)*2))

If lo_client.PostDataStart(ls_url) = 1 Then
    li_PackCount = Round(Len(as_einvoice_json) / 1024, 0)
	For i = 1 To li_PackCount
		ls_NextData = Mid(as_einvoice_json, (i - 1) * 1024 + 1, 1024)
		li_ret = lo_client.PostData(ls_NextData, Len(ls_NextData) * 2)
		If li_ret <> 1 Then Exit
	Next
	li_ret = lo_client.PostDataEnd()
End If

If li_ret = 1 Then
	li_StatusCode = lo_client.GetResponseStatusCode()
	If li_StatusCode = 200 Then
		li_ret = lo_client.GetResponseBody(ls_einvoice_ack_json)
		If li_ret = -1 Then
			ls_einvoice_ack_json = 'General Error'
			ll_return = -1
		ElseIf li_ret = -2 Then
			ls_einvoice_ack_json = 'Code Conversion Failed'
			ll_return = -1
		End if
	Else
		ls_einvoice_ack_json = lo_client.GetResponseStatusText()
		ll_return = -1
	End If
Else
	ls_einvoice_ack_json = of_httperrormessage(li_ret)
	ll_return = -1
End If

I expect the GetResponseBody should give me the result.   What I'm getting is blank string, with return value = -2.
Anything I'm missing?

Happiness Always
BKR Sivaprakash

 

Accepted Answer
Daryl Foster Accepted Answer Pending Moderation
  1. Thursday, 24 March 2022 05:53 AM UTC
  2. PowerBuilder
  3. # Permalink

Hi

Why are you setting the Content-Type to multipart-form if you are just sending json?

I usually just use SendRequest and it works fine.  If I'm sending json I set the Content-Type header to application/json.  I don't even need to add the Content-Length header because it automatically seems to get set.  Here is a simplified example of how I usually post data to my APIs. You can add your other headers and status code checking where appropriate.

 

lnv_HttpClient.SetRequestHeader('Content-Type', 'application/json; charset=utf-8')
// Or without specifying encoding
//lnv_HttpClient.SetRequestHeader('Content-Type', 'application/json')

li_rc = lnv_HttpClient.SendRequest('POST', ls_url, as_einvoice_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, EncodingUTF8!)
    // Or without specifying encoding
    // li_rc = lnv_HttpClient.GetResponseBody(ls_response_body)
else
    // Something went wrong
end if

 

The other thing in your code is that you seem to be using two different variables, ls_einvoice_json and as_einvoice_json which is presumably an argument to the function (unless it is a typo).  Content-Length is set from the local variable but you are posting the argument variable.

Comment
  1. Sivaprakash BKR
  2. Thursday, 24 March 2022 07:08 AM UTC
Thanks Daryl Foster,

Yes the missing one was

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

Setting this solved this issue.



Yes you are right, it's a type. It should be ls_einvoice_json and NOT as_einvoice_json. Issue of copy / paste, modify....

Just this one line, took me a day to figure out.

Thanks again.
  1. Helpful
  1. Daryl Foster
  2. Friday, 25 March 2022 00:07 AM UTC
No worries. Glad it helped.
  1. Helpful
There are no comments made yet.
Arnd Schmidt Accepted Answer Pending Moderation
  1. Wednesday, 23 March 2022 13:01 PM UTC
  2. PowerBuilder
  3. # 1

Hi,

instead of Round() try to use Ceiling()

li_PackCount = Ceiling(Len(as_einvoice_json) / 1024)

Also you should monitor the server to prove that this accepts the unicode encoding.

hth

Arnd

Comment
  1. Sivaprakash BKR
  2. Wednesday, 23 March 2022 13:15 PM UTC
Thanks Arnd,

With Round, it sends the first 1024 characters. My current JSON length is within 1024 characters.

GetResponseStatusCode is 200, GetResponseBody returns -2, with ls_einvoice_ack_json empty value.

Hope GetResponseBody is the method to read the response data, while POSTing.
  1. Helpful
  1. Arnd Schmidt
  2. Wednesday, 23 March 2022 13:32 PM UTC
So the Encoding of the String in the ResponseBody needs to be checked.

You can check the reponse headers and try to add the encoding in the GetResponseBody Method.

Or if you know the Encoding try something like:

lo_client.GetResponseBody(ls_einvoice_ack_json, EncodingUTF8!)



  1. Helpful
  1. Sivaprakash BKR
  2. Wednesday, 23 March 2022 14:10 PM UTC
Thanks Arnd,

1. That URL returns data without encoding the data, I think. I tried with other (Get) APIs, that I could read the result without decoding /encoding.

2. Encoding / Decoding will be helpful only when we read some data from the result. What I get is just a blank string.
  1. Helpful
There are no comments made yet.
Roland Smith Accepted Answer Pending Moderation
  1. Wednesday, 23 March 2022 13:51 PM UTC
  2. PowerBuilder
  3. # 2

Try posting the data as a blob that contains the string in EncodingUTF8! format. Then set the Content-Length and PostData length argument to the length of the blob.

I also don't understand why you are calling PostData in a loop. Why can't you just post the whole thing in one call?

Comment
  1. Arnd Schmidt
  2. Wednesday, 23 March 2022 14:14 PM UTC
Yes! The code examples in the PowerBuilder help for the HTTPClient are not in a good shape and sometimes incomplete.

I wonder if the usage of the RESTClient.SendPostRequest() is the better option if you are dealing with json as request/response.
  1. Helpful
  1. Roland Smith
  2. Wednesday, 23 March 2022 14:14 PM UTC
You can post with the SendRequest function as well. PostData seems to be for uploading really large files.



lblob_Data = Blob(ls_einvoice_ack_json, EncodingUTF8!)



lo_client.SetRequestHeader('Content-Length', String(Len(lblob_Data)))



li_rc = lnv_Http.SendRequest("POST", ls_URL, lblob_Data)

  1. Helpful
  1. Sivaprakash BKR
  2. Wednesday, 23 March 2022 15:05 PM UTC
Thanks Roland,

Tried sending the data (both encoded and without encoding), but the result is empty string. The same json works fine when tried from a website for the testing purpose...

  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.