Receiving JSON from GET request

(8 posts) (2 voices)

Tags:

No tags yet.

  1. Rbert, Member

    Hello,

    There is a feature to obtain the response JSON from a POST request made with dbr.http.post, and save it to the database or call another report with the post_data parameter.

    I need to do a similar thing with a GET request. Is it possible to generalize this functionality to HTTP request types other than POST requests?

    For reference, this is the SQL I am using for the GET request:

      select 'dbr.http.option', 'curl', 'CURLOPT_HTTPHEADER', @auth;
    select 'dbr.http.option', 'curl', 'CURLOPT_HTTPHEADER', 'accept: application/json';
    select 'dbr.http.save', 'sp_save_json';
    select 'dbr.http.get', concat(@url, '/v1/reports/roster');

    Here, the dbr.http.save command seems to have no effect, the response is not saved to the database.

    Thank you!

  2. myDBR Team, Key Master

    When you use dbr.http.save, a linked report is needed to be defined with dbr.report.

    The debug option works now for POST, PUT and GET calls.

    --
    myDBR Team

  3. Rbert, Member

    Ah I understand. Thank you for the reply!

  4. Rbert, Member

    I am now trying to call a linked report like this, using the saved json:

      select 'dbr.http.option', 'curl', 'CURLOPT_HTTPHEADER', @auth;
    select 'dbr.http.option', 'curl', 'CURLOPT_HTTPHEADER', 'accept: application/json';
    select 'dbr.http.save', 'sp_save_json';
    select 'dbr.report', 'sp_DBR_sync_sling_shifts', 'inParamJson=p_json', 'inPostJsonId=id';
    select 'dbr.http.get', concat(@url, '/v1/reports/roster', '?', @dates_param, '&', @v_param), json_object('stage', 'got_shifts') as p_json;

    However, it is failing, with this message as output:

    Warning: Cannot modify header information - headers already sent by (output started at /var/www/corey/apps/showReport.php:7873) in /var/www/corey/lib/utils.php on line 5604

    Do you know what is causing this? Thank you.

  5. myDBR Team, Key Master

    Is that a full report or do you have other commands in the report?

    --
    myDBR Team

  6. Rbert, Member

    This is the full report:

    DROP PROCEDURE IF EXISTS sp_DBR_sync_sling_shifts
    $$
    CREATE PROCEDURE `sp_DBR_sync_sling_shifts`(
    inLogin varchar(256),
    inParamJson text,
    inPostJsonId int(11)
    )
    BEGIN
    declare _stage varchar(45); set @user = 'hidden';
    set @password = 'hidden';
    set @url = 'hidden';
    set @org_id = hidden; if json_valid(inParamJson) and inParamJson ->> '$.stage' is not null then
    set _stage = inParamJson ->> "$.stage";
    else
    set _stage = '';
    end if; if _stage = '' then # get token; set @header_n = FLOOR(RAND() * (10000000)); set @token_url = concat(@url, 'hidden'); select 'dbr.http.option', 'curl', 'CURLOPT_HTTPHEADER', 'Content-Type: application/json';
    select 'dbr.http.header', 'sp_save_http_header', @header_n;
    select 'dbr.http.save', 'sp_save_json';
    select 'dbr.report', 'sp_DBR_sync_sling_shifts', 'inParamJson=p_json', 'inPostJsonId=id';
    select 'dbr.http.post', @token_url, @user as "email", @password as "password", json_object('stage', 'got_token', 'header_id', @header_n) as p_json; elseif _stage = 'got_token' then
    select 'Got Token';
    select inPostJsonId;
    select inParamJson;
    set @header_id = inParamJson ->> "$.header_id"; select * from AhaInfo.http_json where id = inPostJsonId;
    select * from AhaInfo.dbr_http_headers where header_id = @header_id; set @session_url = concat(@url, '/v1/account/session'); set @token = (
    select
    TRIM(
    REPLACE(
    REPLACE(
    SUBSTRING_INDEX(header_item, ' ', -1),
    '\r', ''),
    '\n', '')
    )
    from
    AhaInfo.dbr_http_headers
    where
    header_id = @header_id
    and header_item like 'authorization%'
    ); set @auth = concat('authorization: ', @token); select 'dbr.http.option', 'curl', 'CURLOPT_HTTPHEADER', 'content-type: application/json';
    select 'dbr.http.option', 'curl', 'CURLOPT_HTTPHEADER', 'accept: application/json';
    select 'dbr.http.option', 'curl', 'CURLOPT_HTTPHEADER', @auth; select 'dbr.http.put', @session_url, @org_id as 'orgId', 'silent'; set @dates_param = 'dates=2023-07-01/2023-07-02';
    set @v_param = '2'; select 'dbr.http.option', 'curl', 'CURLOPT_HTTPHEADER', @auth;
    select 'dbr.http.option', 'curl', 'CURLOPT_HTTPHEADER', 'accept: application/json';
    select 'dbr.http.save', 'sp_save_json';
    select 'dbr.report', 'sp_DBR_sync_sling_shifts', 'inParamJson=p_json', 'inPostJsonId=id';
    select 'dbr.http.get', concat(@url, '/v1/reports/roster', '?', @dates_param, '&', @v_param), json_object('stage', 'got_shifts') as p_json; elseif _stage = 'got_shifts' then select 'Got shifts'; select * from AhaInfo.http_json where id = inPostJsonId; end if; END
    $$

    I'm always calling the same report using json for parameters to avoid creating multiple reports, causing clutter.

  7. myDBR Team, Key Master

    Remove selects that do output before the dbr.http.get. PHP cannot do redirect when the output has already been started.

    Btw, it is better to use DECLARE variables instead of user defined variables (@var) in MySQL/MariaDB. With real variables you will get proper datatypes and avoid any risks of overlapping use of user defined variables when you use CALL or functions.

    --
    myDBR Team

  8. Rbert, Member

    Thank you, this fixed the issue.


Reply

You must log in to post.