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

Re: [Ftpapi] Curl vs LIBHTTP



Hi John,

The tool is named HTTPAPI (not 'LIBHTTP', though that would be the default name of the installation library).  You most likely don't need all of the headers you provided -- most of those are not typically used in REST APIs, but I will provide an example that specifies them, anyway, just in case.

For the most part, it will work in HTTPAPI exactly as you expect.  You POST to the URL, and pass the data and content-type as parameters.  to add the additional headers, you have to define an "xproc" (short for "exit procedure", basically its a callback routine) that will be called during the transaction.  That procedure can provide the headers as a parameter.

I've attached an example.

I don't have any way to try this out, so the code is untested -- but, hopefully it'll point you in the right direction.

-SK


On 10/1/2020 4:56 PM, John Place wrote:

I need to access an API and I’ve been using Curl but it isn’t friendly can I do this with your LIBHTTP?

 

I removed some information on purpose FYI

 

curl 'https://api.peachworks.com/v1/accounts//reports/77/run' \ 

      -H 'Connection: keep-alive' \ 

      -H 'Accept: application/json, text/plain, */*' \ 

      -H 'Authorization: ' \ 

      -H 'Content-Type: application/json' \ 

      -H 'Origin: https://my.getbeyond.com' \ 

      -H 'Referer: https://my.getbeyond.com/' \ 

      -H 'Accept-Language: en-US,en;q=0.9' \ 

      --data-binary $'{"params":["locations.id IN (3)","DAY = \'2020-09-28\'"],"report_type":"app","app_id":64}' \ 

      --compressed 

 



**free

ctl-opt dftactgrp(*no) bnddir('HTTPAPI');

/copy httpapi_h

dcl-s data varchar(1000);
dcl-s result varchar(1000);
dcl-s errorMsg varchar(1000);

// ----------------------------------------------
// put the data to be sent into a string
//
//   NOTE: This is just regular RPG... populate
//         the variable any way you wish...
//         I added linefeeds, indenting, etc
//         it a bit easier to read. A good way
//         to create this is with DATA-GEN
// ----------------------------------------------

data = '{+
          "params":[+
            "locations.id IN (3)",+
            "DAY = ''2020-09-28''"+
           ],+
           "report_type":"app",+
           "app_id":64+
         }';


// ----------------------------------------------
// tell HTTPAPI we want to define a procedure that
// will add additional headers to the standard
// protocol ones.
//
// Also tell HTTPAPI not to handle authentication
// since it will be handled by our custom headers
// ----------------------------------------------

http_xproc( HTTP_POINT_ADDL_HEADER: %paddr(addHeaders) );

http_setAuth( HTTP_AUTH_NONE: '': '');


// ----------------------------------------------
// run the POST request to the URL.
// ----------------------------------------------

monitor;
  result = http_string( 'POST'
           : 'https://api.peachworks.com/v1/accounts/reports/77/run'
           : data
           : 'application/json');
on-error;
  errorMsg = http_error();
  // send message to user or whatnot
endmon;

*inlr = *on;


dcl-proc addHeaders;

  dcl-pi *n;
    newHeaders varchar(32767);
  end-pi;

  dcl-c CRLF x'0d25';

  newHeaders = 'Connection: keep-alive' + CRLF
             + 'Accept: application/json,text/plain,*/*' + CRLF
             + 'Authorization: ' + CRLF
             + 'Origin: https://my.getbeyond.com' + CRLF
             + 'Referrer: https://my.getbeyond.com' + CRLF
             + 'Accept-Language: en-US,en;q=0.9' + CRLF;

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