[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Ftpapi] In: Re: In: HTTPAPI - Example 7 - Upload a file from IFS - No file attached!



Hi Scott,
sorry but I can't run it successfully yet.

I specified the type now but I received again the messagge "No file attached" in your example.

However, using your example 7, I wrote my RPG to sent a PDF document to another webserver.
I had to use an API of IBM food trust system, called Document API, for send my document and I have to use this CURL:
curl -X POST "https://sandbox.food.ibm.com/ift/api/documents/v1/documents" -H  "accept: application/json" -H  "Authorization: Bearer <token>" -H  "Content-Type: multipart/form-data" -F "properties={"documentType": "Generic Document","documentTitle": "UOVA OVI BIO X6-Lotto 1588-20210609","issueDate": "2021-06-09","epcList":["urn:epc:class:lgtin:8054729.020969.1588"]}" -F "entitlement={"entitlementMode": "private",  "entitledOrgIds": []}" -F "content=@Certificato.pdf;type=application/pdf"

In my IFS I have this path, with the PDF document I have to sent:
"/IFT/IFT-SAS/token/Certificato.pdf'

My RPG is:

**free
//* This example demonstrates posting multipart/form-data to a
//* web server.
//*
//* It is intended to mimic the following HTML form:
//*----------------------------------------------------------------
//*
//* <form method="post" enctype="multipart/form-data"
//*       action="">http://www.scottklement.com/httpapi/upload.php">
//*
//*   <input type="hidden" name="operation" value="VERIFY">
//*
//*   File Format:
//*   <input type="radio"  name="data_format" value="HTML" checked>HTML
//*   <input type="radio"  name="data_format" value="PDF">PDF
//*   <input type="radio"  name="data_format" value="PTF">RTF<br>
//*
//*   File to send:
//*   <input type="file" name="handout"><br>
//*
//*   <input type="submit">
//*
//* </form>
//*
//*----------------------------------------------------------------
//*
//H DFTACTGRP(*NO) BNDDIR('HTTPAPI')
ctl-opt copyright('SAS informatica srl')
dftactgrp(*NO) actgrp(*new) bnddir('HTTPAPI') option(*srcstmt);
//*------------------------------------------------------------------------
//* Entry Plist
//*------------------------------------------------------------------------
dcl-pi EntryPlist extpgm('EXAMPLE7');

end-pi;
//*------------------------------------------------------------------------
//* Read JSON IFT Token
//*------------------------------------------------------------------------
dcl-pr leggo_token_IFT extpgm('IUTK3R');
       $acronimo         char(3);
       r_json            char(3000);
end-pr;


//*------------------------------------------------------------------------*
//* /COPY                                                                  *
//*------------------------------------------------------------------------*

/copy qrpglesrc,httpapi_h
/copy qrpglesrc,ifsio_h


dcl-s tempFile                    varchar(10000);
dcl-s ContentType                 char(100);
dcl-s enc                         pointer;
dcl-s msg                         char(52);
dcl-s rc                          int(10:0);
dcl-s $file_path                  varchar(1000);
dcl-s $url                        varchar(1000);
dcl-s $properties                 varchar(1000);
dcl-s $entitlement                varchar(1000);
dcl-s $result                     varchar(1000);
dcl-s $acronimo                   char(3);
dcl-s r_json                      char(3000);

                       
  http_debug(*on);
    *inlr = *on;


    $acronimo = 'SAS';

    // json of IFT token
    leggo_token_IFT($acronimo : r_json);

    // UTF-8 encoding
    http_setCCSIDs(1208: 0);

    http_xproc( HTTP_POINT_ADDL_HEADER
        : %paddr(tripHeaders) );


    //  Ask HTTPAPI for a temporary filename that won't
    //  conflict with another job.
    $file_path = '/IFT/IFT-SAS/token/Certificato.pdf';

    ContentType = 'multipart/form-data';
    tempFile = http_tempfile();

    //  HTTPAPI's multipart/form-data encoding functions output
    //    two things:
    //
    //       1) A stream file field suitable for use with
    //            http_url_post_stmf()
    //       2) A content-type field suitable for use with
    //            http_url_post_stmf()
    //
    //  The http_mfd_encoder_open() API opens the stream file
    //  and initializes the encoding routines.  You must call
    //  that first:

    enc = http_mfd_encoder_open( tempFile : ContentType );
    if (enc = *NULL);
       msg = http_error();
       dsply msg;
       return;
    endif;

    //
    //  now you can add variables to the encoded stream file:
    //

     $properties = '{"documentType": "Generic Document", ' +
     '"documentTitle": "UOVA OVI BIO X6 - Lotto 1588 - 20210609", ' +
     '"issueDate": "2021-06-09", ' +
     '"epcList":["urn:epc:class:lgtin:8054729.020969.1588"]}';

     $entitlement = '{"entitlementMode": "private", ' +
     '"entitledOrgIds":[]}';

     http_mfd_encoder_addvar_s(enc: 'properties'  : $properties);
     http_mfd_encoder_addvar_s(enc: 'entitlement'  : $entitlement);

    //
    //  and you can even add the contents of another stream
    //  file, compatible with the <input type="file"> HTML
    //  keyword.
    //
    //  In this case, the HTTPAPI handout that was downloaded
    //  in EXAMPLE1 will be added to the temp file
    //

    http_mfd_encoder_addstmf( enc
                            : 'Certificate'
                            : $file_path
                            : 'application/pdf');

    //
    // once all of the variables/files have been added, the
    // http_mfd_encoder_close() API must be called to clean
    // up. (The stream file will remain on disk so that you
    // can use it with http_url_post_stmf)
    //

    http_mfd_encoder_close( enc );


    //
    //  post the results to the web server
    //

    $url = ''">https://sandbox.food.ibm.com/ift/api/documents/v1/documents';
    $result = '/IFT/IFT-SAS/token/result_certificate.txt';

    rc = http_url_post_stmf($url
                           : tempFile
                           : $result
                           : HTTP_TIMEOUT
                           : HTTP_USERAGENT
                           : ContentType );

    if (rc <> 1);
       msg = http_error();
       dsply msg;
       return;
    endif;


    //
    //  delete temp files, we're done
    //

    unlink($result);
    unlink(tempFile);

    return;

    //==============================================================================
    // Add headers
    //==============================================================================
    dcl-proc tripHeaders;
    dcl-pi tripHeaders;
       Header   varchar(3000);
       UserData pointer value;
    end-pi;

    Header = 'Accept: application/json'
           + x'0d25'
           + 'Authorization: Bearer ' + %trim(r_json)
           +  x'0d25';
    end-proc;
                                 

In this case I don't receive the error "No file attached!", but there is something strange.
In the document list of the IBM food trust library, I find the document as a title, but when I try to view the detail and open the PDF file, I don't find the document.
It seems the same result... that the file was not attached in fact.
Can you help me, please?
Thank you very much in advance.


_________________________________________________________
Sara Bellucci

SAS INFORMATICA srl

Tel: +39 075 5058160
Fax: +39 075 5058105
e-mail: SaraBellucci@xxxxxxxxxxxxxxxxx
http://www.sasinformatica.it



Da:        "Scott Klement" <sk@xxxxxxxxxxxxxxxx>
Per:        SaraBellucci@xxxxxxxxxxxxxxxxx
Data:        16/06/2021 21:15
Oggetto:        Re: [Ftpapi] In: HTTPAPI - Example 7 - Upload a file from IFS - No file attached!




Hi Sara!
"No File Attached!" is not coming from HTTPAPI.  Most likely, when you send your request to the HTTP server (REST API or whatever you're calling) it doesn't recognize the format of the document you're sending.   All web services are different, so it is important to send things in the format that's required by the specific one that you are calling.
--
Scott Klement
sk@xxxxxxxxxxxxxxxx
On 6/16/2021 5:00 AM, SaraBellucci@xxxxxxxxxxxxxxxxx wrote:
Hello,

I need to send a PDF file from IFS to a server by calling an IBM webservice.

I saw the EXAMPLE7 of the HTTPAPI tool, and I tryed to use this but I received the error "No File Attached!".

This is my RPG code to test the upload of a test PDF file:




-- 
_______________________________________________
Ftpapi mailing list
Ftpapi@xxxxxxxxxxxxxxxxxxxxxx
http://scottklement.com/mailman/listinfo/ftpapi