1. SHAMEEM KAKKAD
  2. PowerBuilder
  3. Monday, 10 July 2023 11:50 AM UTC

Hi,

I want to send a file through the httpclient method.. any body can help ?. Below the curl file. which is working from post man

 

curl -X POST "https://tmbapi.riayati.ae:8084/api/Attachments/UploadAttachment" -H "accept: text/plain" -H "username: 111" -H "password: 222222" -H "Content-Type: multipart/form-data" -F "file=@3851.pdf;type=application/pdf"

 

Who is viewing this page
Accepted Answer
René Ullrich Accepted Answer Pending Moderation
  1. Tuesday, 11 July 2023 11:17 AM UTC
  2. PowerBuilder
  3. # Permalink
403 means that the server understands the request but won't fulfill it. Maybe there is a permission problem. You should ask the server admin. Because you use request header to send username and password it may be that the server may respond with 403 if they are invalid? (Usually servers responds with 401 in this case).
Comment
  1. SHAMEEM KAKKAD
  2. Wednesday, 12 July 2023 04:58 AM UTC
Sir, I give the boundary value ------WebKitFormBoundary7MA4YWxkTrZu0gW--

Now I can upload the file successfully...

This boundary value got from Postman...



Actually what is the meaning of this value ?
  1. Helpful
  1. René Ullrich
  2. Wednesday, 12 July 2023 05:38 AM UTC
The boundary value is a delimiter for the different parts (form data, on or more files) of the message body. So it must be a value that is not part of the other data.
  1. Helpful
  1. SHAMEEM KAKKAD
  2. Wednesday, 12 July 2023 08:03 AM UTC
Thanks
  1. Helpful
There are no comments made yet.
Daryl Foster Accepted Answer Pending Moderation
  1. Tuesday, 11 July 2023 03:34 AM UTC
  2. PowerBuilder
  3. # 1

Hi Shameem,

 

It looks like it is just a simple multi-part form upload which you'll find heaps of information about.  The link below has almost the exact code you need to post a file as a multipart form. 

https://community.appeon.com/index.php/qna/q-a/snap-develop-api-file-upload-download#reply-28996

Normally for authentication a form upload would use Basic Auth, but for this one it looks like they just add a couple of extra headers for username and password.  You'll need to add that to the code in the example where the other header is set add these two lines (with your username and password).

lnv_httpClient.SetRequestHeader("username", "user")

lnv_httpClient.SetRequestHeader("password", "pass")

You'll also need to rename the uploadedFile in this statement to file and maybe also change invoice.pdf to the actual filename of the invoice

 

blb_file = blob( 'Content-Disposition: form-data; name="uploadedFile"; filename="invoice.pdf"~r~n' + &
                        "Content-Type: application/pdf~r~n~r~n", EncodingUTF8!) + &
                        blb_file

 

You'll also need to set ls_endpoint to the url of your api

ls_endpoint = 'http://localhost:5000/api/file/upload'

 

Comment
  1. SHAMEEM KAKKAD
  2. Tuesday, 11 July 2023 09:25 AM UTC
Sir, I am getting this response.. But file is there

{

"StatusCode": 400,

"Message": "The attachment is empty.",

"Success": false,

"UserMessage": "Attachment not found.",

"MemberValidation": null,

"Error": []

}



My script is....I am very poor in PB... Sorry...

---------------------

string response,link_,filename_

link_ = 'https://tmbapi.riayati.ae:8084/api/Attachments/UploadAttachment'

filename_ = 'D:\Ry-Shade\DSfiles\3851.pdf'



blob blb_file



HttpClient shahttpclient

shahttpclient = Create HttpClient

shahttpclient.clearrequestheaders()

shahttpclient.SetRequestHeader("Accept", "text/plain")

shahttpclient.SetRequestHeader("username", 'TEST024')

shahttpclient.SetRequestHeader("password", '55a4e6ec-06d0-4bf8-a3c2-d4c402afdb4b')

//shahttpclient.SetRequestHeader("Content-Type", "multipart/form-data")



blb_file = blob( 'Content-Disposition:multipart/form-data; name="File"; filename="'+filename_+'"~r~n' + &

"Content-Type: application/pdf~r~n~r~n", EncodingUTF8!)// + blb_file



res_ = shahttpclient.SendRequest('POST', link_,blb_file)

if res_ = 1 then

ret_ = shahttpclient.GetResponseStatusCode()

messagebox ("ret",string (ret_))

shahttpclient.GetResponseBody(response)

else

if res_ = -1 then

response = 'General error'

elseif res_ = -2 then

response = 'Invalid URL'

elseif res_ = -3 then

response = 'Cannot connect to the Internet'

elseif res_ = -4 then

response = 'Timed out'

end if

end if

messagebox ("Info",response)
  1. Helpful
  1. René Ullrich
  2. Tuesday, 11 July 2023 10:11 AM UTC
Check the example in the linked thread. You have to read the file and build a blob to send. Daryl has only specified here the changes you have to made!
  1. Helpful
  1. SHAMEEM KAKKAD
  2. Tuesday, 11 July 2023 10:46 AM UTC
Yes sir, I give that codes with you mentioned changes.. But while give that codes getting below response



Sir , Please help me...





<h1>

403 Forbidden</h1>

Request forbidden by administrative rules.





--------------------------------------------------------

long ll_FileNum

blob blb_file

blob blb_boundary

blob blb_terminus

blob blb_multipart

integer li_rc



HTTPClient lnv_HttpClient



string ls_endpoint

string ls_file

integer li_ResponseStatusCode = 0

string ls_ResponseBody = ''

string ls_ResponseStatusMessage = ''





// Hard code the file name and endpoint for testing

ls_file = 'D:\Ry-Shade\DSfiles\3852.pdf'

ls_endpoint = 'https://tmbapi.riayati.ae:8084/api/Attachments/UploadAttachment'



// Create a boundary marker for the multipart blob

string ls_Boundary = "$$$Boundary$$$"



// Read the file into a blob

ll_FileNum = FileOpen(ls_file, StreamMode!)

if ll_FileNum > 0 then

FileReadEx(ll_FileNum, blb_file)

FileClose(ll_FileNum)

else

MessageBox('File Error', 'Error Opening Filename ' + ls_file)

return

end if



// Create component blobs

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

blb_file = blob( 'Content-Disposition: form-data; name="File"; filename="3852.pdf"~r~n' + &

"Content-Type: application/pdf~r~n~r~n", EncodingUTF8!) + &

blb_file

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



// Concatenate blobs into a single multipart blob

blb_multipart = blb_boundary + blb_file + blb_terminus



// Send the request

lnv_HttpClient = create HTTPClient

lnv_httpClient.SetRequestHeader("Content-Type", "multipart/form-data; boundary=" + ls_BOUNDARY)

lnv_httpClient.SetRequestHeader("username", '111') //TEST024

lnv_httpClient.SetRequestHeader("password", '1111111')



li_rc = lnv_httpClient.SendRequest('POST', ls_endpoint, blb_multipart)

// obtain the response data

if li_rc = 1 then

li_ResponseStatusCode = lnv_HttpClient.GetResponseStatusCode()

ls_ResponseStatusMessage = lnv_HttpClient.GetResponseStatusText()

lnv_HttpClient.GetResponseBody(ls_ResponseBody, EncodingUTF8!)



if li_ResponseStatusCode = 200 then

MessageBox('Upload Successfull', ls_ResponseBody)

else

MessageBox('Upload Failed', ls_ResponseBody)

end if

else

MessageBox('HTTP Error', 'Error calling ' + ls_endpoint + '. Return Code ' + string(li_rc))

end if

  1. Helpful
There are no comments made yet.
Roland Smith Accepted Answer Pending Moderation
  1. Monday, 10 July 2023 18:41 PM UTC
  2. PowerBuilder
  3. # 2

Here is an example. The function ReadBinaryFile is a function in the same object that uses FileOpen/FileReadEx/FileClose to read the file into a blob.

HttpClient lnv_Http
Constant Long BlobPartLen = 65536
Blob lblb_PostData, lblb_PostPart
Integer li_rc, li_PartNum
Long ll_StatusCode, ll_Bytes
String ls_ServerURL, ls_RecvData
String ls_PathName, ls_FileName, ls_StatusText

li_rc = GetFileOpenName("Select a File", ls_PathName, ls_FileName)
If li_rc < 1 Then Return

mle_response.Text = ""
cb_openfile.Enabled = False
FileDelete(is_FileName)

Yield()

SetPointer(HourGlass!)

// read the file
ll_Bytes = ReadBinaryFile(ls_PathName, lblb_PostData)
If ll_Bytes = 0 Then
	MessageBox(this.Text, &
					"ReadBinaryFile Failed!", StopSign!)
	Return
End If

// set the URL
ls_ServerURL = "http://localhost/pbhttp/webservice.pbh"

// create the object
lnv_Http = Create HttpClient

// set headers
lnv_Http.SetRequestHeader("FileName", ls_FileName)
lnv_Http.SetRequestHeader("Content-Length", String(Len(lblb_PostData)))

// send the request
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
		lnv_Http.GetResponseBody(ls_RecvData)
		mle_response.text = ls_RecvData
	Else
		lnv_Http.GetResponseBody(ls_RecvData)
		mle_response.text = ls_RecvData
		ls_StatusText = lnv_Http.GetResponseStatusText()
		MessageBox("SendRequest Error #" + String(ll_StatusCode), &
						ls_StatusText, StopSign!)
	End If
Else
	MessageBox(this.Text, &
					"PostDataStart Error: " + String(li_rc), StopSign!)
End If

// destroy the object
Destroy lnv_Http
Comment
  1. Roland Smith
  2. Monday, 10 July 2023 18:44 PM UTC
This code transmits the file in 65536 byte chunks. This is how the PB help file does it. The SendRequest function can send data in one piece but it is limited to about 20MB.
  1. Helpful
  1. SHAMEEM KAKKAD
  2. Tuesday, 11 July 2023 09:26 AM UTC
Hi Roland, Also I used above script... but getting same error



{

"StatusCode": 400,

"Message": "The attachment is empty.",

"Success": false,

"UserMessage": "Attachment not found.",

"MemberValidation": null,

"Error": []

}
  1. Helpful
There are no comments made yet.
SHAMEEM KAKKAD Accepted Answer Pending Moderation
  1. Monday, 10 July 2023 15:55 PM UTC
  2. PowerBuilder
  3. # 3

this is C# example which provided by API team

 

Comment
There are no comments made yet.
René Ullrich Accepted Answer Pending Moderation
  1. Monday, 10 July 2023 12:32 PM UTC
  2. PowerBuilder
  3. # 4
Comment
  1. SHAMEEM KAKKAD
  2. Monday, 10 July 2023 16:31 PM UTC
Hi Sir,

I can't understand this articles. Can you provide the PB code ?. Above mentioned the C# code
  1. Helpful
  1. Daryl Foster
  2. Tuesday, 11 July 2023 03:21 AM UTC
The links that René posted above should give you all you need to work it out, but I'll post a link to another post which will have almost the exact code you need.
  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.