PxPlus User Forum

Twitter Twitter Twitter

Author Topic: Posting a File to a Web Service  (Read 45 times)

Jeffrey Ferreira

  • Diamond Member
  • *****
  • Posts: 179
  • I must have taken a wrong turn at Albuquerque.
    • View Profile
Posting a File to a Web Service
« on: May 17, 2024, 09:47:02 PM »
Hi List,
in the past i could never get *plus/web/request work with uploading a file.
i can get it to work in postman, curl and powershell....but i cannot get syntax right in pxplus
i'm referring to line 100 .
does anyone know what i'm doing wrong or has used *plus/web/request to post/upload file
i dont want to use a 3rd party utility if i can help it

0040 let RAW_CREDENTIALS$="username:password"
0050 call "*web/base64;ENCODE_STR",RAW_CREDENTIALS$,BASE64_CREDENTIALS$
0060 let EXTRA_HEADERS$="Authorization: Basic "+BASE64_CREDENTIALS$
0070 let MIME_TYPE$="multipart/form-data"
0080 let URI$="https://staging.saberis.com:9000/api/v1/documents/modules/21/upload"
0100 let BODY$="filename=Cleary_Quote.xml"
0110 call "*plus/web/request",URI$,BODY$,RESPONSE$,RESPONSE_HEADER$,MIME_TYPE$,"",EXTRA_HEADERS$


thanks

jeff

Stéphane Devouard

  • Diamond Member
  • *****
  • Posts: 124
  • PxPlus guru with skills in PHP, JS, C#, Java
    • View Profile
    • Stéphane's Web Resume
Re: Posting a File to a Web Service
« Reply #1 on: Today at 03:47:31 AM »
Jeff

Attached is the PVX helper class I use to upload files to APIs

Use it like this :

Code: [Select]
field_name$="Name of the form field the webservice is waiting for"
file_name$="Name of the file to upload : this will be the value of the form field above"
file_content$="File content"
file_mime_type$="MIME type of the file to upload, application/octet-stream by default"
upload=new("http.upload",field_name$,file_name$,file_content$,file_mime_type$)
! // use upload'content_type$ as mime_type in the *plus/web/request
! // use upload'body$() as body in the *plus/web/request

The file_content$ variable should be loaded with a binary read of the file

Code: [Select]
open (hfn,isz=-1) file_name$
file_content$ = rcd(lfo,siz=10000000)

Hope this helps
Stéphane Devouard
Portfolio | Work

Mike King

  • Diamond Member
  • *****
  • Posts: 3828
  • Mike King
    • View Profile
    • BBSysco Consulting
Re: Posting a File to a Web Service
« Reply #2 on: Today at 09:51:16 AM »
Jeff,

There is a utility built into PxPlus that will load a variable with the contents of a file:  *tools/readfile.

https://manual.pvxplus.com/PXPLUS/utilities/readfile.htm

Given this all you need to do is change line 100 in your code to something like:

0100 call "*tools/readfile", "Cleary_Quote.xml", Body$
Mike King
President - BBSysco Consulting
eMail: mike.king@bbsysco.com