1. TOMAZ KRALJ
  2. PowerBuilder
  3. Thursday, 6 July 2023 10:47 AM UTC

Hello,

PB 2021 - 1509

Is it possible to send files with REST client or do I have to use Http client? If I want to use RESTclient should I convert blob to string and then send this string with SendPostRequest function?

Thank you for your answers and advices!.

 

Tomaž

Accepted Answer
TOMAZ KRALJ Accepted Answer Pending Moderation
  1. Wednesday, 12 July 2023 13:52 PM UTC
  2. PowerBuilder
  3. # Permalink

I got it working!

Here is my sample code if anyone need it one day. It is result of reading some others posts...

-----  Upload PDF file using HttpClient -----------

 


string ls_url,ls_status_text,ls_json,ls_odgovor,ls_rezultat,ls_key,ls_datum
integer li_rezultat,li_fileclose
long ll_status_code,ll_root,ll_stevilo,ll_index,ll_filelength
long ll_ChildArray
JSONParser ljson_pa
long ll_FileNo
blob blb_boundary,blb_terminus,blb_filename,blb_multipart
blob lb_file
string ls_napaka_name,ls_napaka_message,ls_napaka_code,ls_napaka_status, ls_conv_lb_file
string ls_BOUNDARY = "xxxxxxxxxx"

 

if isvalid(inv_HttpClientT) then
else
inv_HttpClientT = create httpclient
end if

if ii_tessera_connected=1 then

else
//of_set_login()

end if

ls_url=BaseUrl+'/api/v1/documents/upload'

 

ljson_pa= create JSONParser

inv_HttpClientT.clearrequestheaders( )
inv_HttpClientT.SetRequestHeader ("Content-Type", "multipart/form-data; boundary="+ls_BOUNDARY)
inv_HttpClientT.SetRequestHeader ("Authorization", "Bearer "+is_tessera_Bearer)


//read file in blob and close it

ll_FileNo=fileopen(as_filepath,StreamMode!)

ll_filelength=FileReadEx(ll_FileNo,lb_file)

li_fileclose=FileClose(ll_FileNo)


blb_boundary =blob('--' + ls_Boundary + '~r~n', EncodingUTF8!)

blb_terminus = blob('~r~n--' + ls_Boundary + '--~r~n', EncodingUTF8!)

blb_filename = blob('Content-Disposition: form-data; name="file"; filename='+ as_FileName+'~r~n' + "Content-Type: application/pdf~r~n~r~n", EncodingUTF8!) + lb_file

// Concatenate blobs into a single multipart blob

blb_multipart = blb_boundary + blb_filename + blb_terminus


li_rezultat=inv_HttpClientT.sendRequest( "POST",ls_url,blb_multipart)

ll_status_code=inv_HttpClientT.getresponsestatuscode( )
ls_status_text=inv_HttpClientT.getresponsestatustext( )

inv_HttpClientT.GetResponseBody(ls_odgovor)

ls_rezultat = ljson_pa.LoadString(ls_odgovor)


if ll_status_code=204 then //ok
RETURN 1

else //errors

//read JSON
ll_root = ljson_pa.GetRootItem()

ll_stevilo=ljson_pa.getchildcount( ll_root)

for ll_Index = 1 to ll_stevilo
ls_key = ljson_pa.GetChildKey(ll_root, ll_Index)

if ls_key='name' then
ls_napaka_name=ljson_pa.getitemstring( ll_root,ls_key)
elseif ls_key='message' then
ls_napaka_message=ljson_pa.getitemstring( ll_root,ls_key)
elseif ls_key='code' then
ls_napaka_code=ljson_pa.getitemstring( ll_root,ls_key)
elseif ls_key='status' then
ls_napaka_status=ljson_pa.getitemstring( ll_root,ls_key)
end if
next


if isnull(ls_napaka_code) then ls_napaka_code=''
if isnull(ls_napaka_name) then ls_napaka_name=''
if isnull(ls_napaka_message) then ls_napaka_message=''
if isnull(ls_napaka_status) then ls_napaka_status=''

Messagebox('Error','Ime napake: '+ls_napaka_name +'~r~n' + &
'Opis napake: '+ls_napaka_message+'~r~n' + &
'Coda napake: '+ls_napaka_code+'~r~n' + &
'Status napake: '+ls_napaka_status )


return -1

 

end if

 

 

Comment
  1. René Ullrich
  2. Wednesday, 12 July 2023 14:00 PM UTC
You should be sure that ls_BOUNDARY may not be part of the other part (pdf file) that you send.
  1. Helpful
  1. Roland Smith
  2. Wednesday, 12 July 2023 17:55 PM UTC
The boundary should be a random string of letters or numbers that wouldn't appear in the file. Maybe use the CoderObject Base64Encode function to encode the file name.
  1. Helpful
There are no comments made yet.
Roland Smith Accepted Answer Pending Moderation
  1. Thursday, 6 July 2023 13:03 PM UTC
  2. PowerBuilder
  3. # 1

Here is a cut-down version of code I am using to post files using HttpClient. The file blob (lblb_PostData) is sent in 65536 byte chunks.

The PostDataStart function makes the connection with the server. The PostData function sends the data. The PostDataEnd lets the server know that the data is complete. The processing of the web service occurs during the PostDataEnd function.

HttpClient lnv_Http
Constant Long BlobPartLen = 65536
Blob lblb_PostData, lblb_PostPart
Integer li_rc, li_PartNum
String ls_ServerURL

li_rc = lnv_Http.PostDataStart(ls_ServerURL)
If li_rc = 1 Then
	// post the data in parts
	do while li_rc = 1
		lblb_PostPart = BlobMid(lblb_PostData, li_PartNum * BlobPartLen + 1, BlobPartLen)
		li_rc = lnv_Http.PostData(lblb_PostPart, BlobPartLen)
		li_PartNum ++
	loop
	lnv_Http.PostDataEnd()

	// get results
	ll_StatusCode = lnv_Http.GetResponseStatusCode()

	If ll_StatusCode = 200 Then
		// HTTP status 200 = OK
	Else
	End If
End If
Comment
There are no comments made yet.
René Ullrich Accepted Answer Pending Moderation
  1. Thursday, 6 July 2023 11:30 AM UTC
  2. PowerBuilder
  3. # 2

You can send files with HTTPClient.

The usual way: (depends what your server want ;-))

https://community.appeon.com/index.php/qna/q-a/http-client-multipart-response-body?limitstart=0#reply-27148

 

Comment
There are no comments made yet.
Sivaprakash BKR Accepted Answer Pending Moderation
  1. Thursday, 6 July 2023 11:17 AM UTC
  2. PowerBuilder
  3. # 3

Though I've not transferred files, I think you can try with HTTPClient.

 

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.