1. Rodrigo Rosa
  2. PowerBuilder
  3. Tuesday, 3 November 2020 23:01 PM UTC

Hi everione,

 

I need send formData in httpclient.setrequestheader

 

has anyone done this?

My code:

long
ll_return
string
lb_file
//bloob
lb_file

http.setrequestheader( 'Cache-Control', 'no-cache')
//http.setrequestheader('Content-Type', 'multipart/form-data')
http.setrequestheader('Content-Type', 'application/json')
http.setrequestheader( 'If-Modified-Since', 'Thu, 1 Jan 1970 00:00:00 GMT')
http.setrequestheader( 'Authorization', 'bearer ' + ls_token)

ll_file = fileopen('C:\Users\rodrigo.rosa\Documents\file_send.txt', textmode!)
li_bytes = FileReadex(ll_arquivo, lb_file)
fileclose(ll_arquivo)

http.setrequestheader( 'file', (lb_file))
ll_return = http.SendRequest('PUT', ls_url)
Armeen Mazda @Appeon Accepted Answer Pending Moderation
  1. Tuesday, 3 November 2020 23:13 PM UTC
  2. PowerBuilder
  3. # 1

You need to Base64 encode files.  You cannot send Blob in JSON.  JSON is a string.

I think you would really benefit from going through the REST/JSON Bootcamp: 

https://www.appeon.com/developers/library/videos/rest-bootcamp.html

https://www.appeon.com/developers/library/videos/enhanced-restclient-powerbuilder-2019.html

 

Comment
  1. Rodrigo Rosa
  2. Tuesday, 10 November 2020 15:02 PM UTC
hi, even coding keeps returning as error 500
  1. Helpful
There are no comments made yet.
Daryl Foster Accepted Answer Pending Moderation
  1. Wednesday, 4 November 2020 06:09 AM UTC
  2. PowerBuilder
  3. # 2

Hi Rodrigo, it depends on the specification of the API you are trying to send to, but have a look at this link and see if it helps:

 

https://community.appeon.com/index.php/qna/q-a/httpclient-post-with-multipart-form

 

The author had a similar question and at the bottom of the post he shows how he did it.  It shows how to upload a file as a multipart/form-data POST.  I think it should be the same concept when using the PUT method. 

If this is what you are looking for, the form data isn't sent in the header.  The header just specifies that it is sending "multipart/form-data".  The actual form data is send in the body made up of a bunch of blobs concatenated together separated by a boundary.   You don't need to base64 encode the file with this method.

Comment
  1. Rodrigo Rosa
  2. Tuesday, 10 November 2020 15:01 PM UTC
hi, i tried this solution but without success yet
  1. Helpful
There are no comments made yet.
Rodrigo Rosa Accepted Answer Pending Moderation
  1. Tuesday, 10 November 2020 15:11 PM UTC
  2. PowerBuilder
  3. # 3

the code based on the help received:

 

ll_arquivo = fileopen('C:\@Trabalho\test_file.txt', StreamMode!)

li_bytes = FileReadex(ll_arquivo, lb_file)
fileclose(ll_arquivo)

ls_FileName = 'test_file.txt'
ls_MimeType = 'application/text'
ls_BOUNDARY = "$$$Boundary$$$"

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

blb_filename = blob('Content-Disposition: form-data; name="File-Name"' + '~r~n~r~n' + ls_FileName, EncodingUTF8!)
blb_mimetype = blob('Content-Disposition: form-data; name="Mime-Type"' + '~r~n~r~n' + ls_MimeType, EncodingUTF8!)

lb_file = blob( 'Content-Disposition: form-data; name="File"; filename="' + ls_FileName + '"' + '~r~n' + &
"Content-Type: " + ls_MimeType + "~r~n~r~n", EncodingUTF8!) + &
lb_file

blb_multipart = blb_boundary + &
                       blb_filename + &
                      blb_mimetype + &
                       blb_boundary + &
                       lb_file + &
                       blb_terminus

ls_base64 = lnv_CoderObject.base64encode(lb_file)

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

ll_retorno = http.SendRequest('PUT', ls_url, ls_base64)

 

Even so, I continue with the error 500

 

Comment
  1. Daryl Foster
  2. Tuesday, 10 November 2020 20:15 PM UTC
I’ll have a proper look when I’m on my PC. But you need to send

blb_multipart and not ls_base64. You shouldn’t need to base64 with a multipart form. You also need to make sure you are setting your authorisation header which you showed in your original question.
  1. Helpful
There are no comments made yet.
Daryl Foster Accepted Answer Pending Moderation
  1. Tuesday, 10 November 2020 23:45 PM UTC
  2. PowerBuilder
  3. # 4

I can see a couple of issues.  I'm assuming there is an API specification that you are following that tells you what form fields they are expecting? For examples these two lines:

blb_filename = blob('Content-Disposition: form-data; name="File-Name"' + '~r~n~r~n' + ls_FileName, EncodingUTF8!)
blb_mimetype = blob('Content-Disposition: form-data; name="Mime-Type"' + '~r~n~r~n' + ls_MimeType, EncodingUTF8!)

Does the specification say they are expecting File-Name and Mime-Type?  Does the specification say to use the http PUT method rather than POST?

Also each part of the form data needs to be seperated with the boundary, so there needs to be a boundary between blb_filename and blb_mimetype

blb_multipart = blb_boundary + &
                       blb_filename + &
                      blb_mimetype + &
                       blb_boundary + &
                       lb_file + &
                       blb_terminus

should be

blb_multipart = blb_boundary + &
                       blb_filename + &
                       blb_boundary + &
                      blb_mimetype + &
                       blb_boundary + &
                       lb_file + &
                       blb_terminus


If they are expecting a token you need to add the authorization headers and if you are expecting json to be returned you can add a json accept header.  These are the lines I use in my code:

lnv_HttpClient = create HTTPClient
lnv_HttpClient.SetRequestHeader('Authorization', is_token_type + ' ' + is_Token )
lnv_HttpClient.SetRequestHeader('Accept', 'application/json')
lnv_httpClient.SetRequestHeader("Content-Type", "multipart/form-data; boundary=" + ls_BOUNDARY)

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

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.