ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

HTTPAPI: Request Headers not updating

Collapse
This topic is closed.
X
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • RichDotson
    replied
    Never mind. I was able to get this working!

    Leave a comment:


  • RichDotson
    started a topic HTTPAPI: Request Headers not updating

    HTTPAPI: Request Headers not updating

    I wrote two programs using the HTTPAPI library. The first signs into our Saleforce instance and retrieves an oAuth access_token. This is working great.

    The second program takes the access_token and tries to send a JSON payload the Salesforce REST API. This is where I am having an issue.

    Below is the section of code that calls http_xproc to set the additional headers. That call doesn't seem to be working. When I run in debug and display the "header" variable it contains the following data.

    Content-Type: text/plain;charset=UTF-8
    Authorization: Bearer 00DXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX <--- Has the access_token from first program call


    I look at the http_debug log and it appears that the header is not getting set because the Authorization appears as Basic and it is returning a 401 Unauthorized.

    POST /services/apexrest/ir-order-status-receiver/bulk HTTP/1.1
    Host: XXXXXXXXXXX.salesforce.com
    User-Agent: http-api/1.39
    Content-Type: application/x-www-form-urlencoded
    Content-Length: 48
    Authorization: Basic aXNlcmllcy5XXXXXXXXXXXXxMTlqaGth

    senddoc(): entered
    {"message":"first test order","order":"9876543"}
    recvresp(): entered
    HTTP/1.1 401 Unauthorized



    I can take the url, header and postJSON from debug and send it Salesforce using ARC and I get the correct response. The postJSON file is sent to Salesforce as expected.

    When I run the RPGLE code below I get the "This page requires a user-id & password" error message.

    I included more of the http_debug Log below.

    Any help will be appreciated.

    Thanks,
    Rich


    // JSON to be loaded into Salesforce
    postJSON = '{"message":"first test order","order":"9876543"}';

    http_setCCSIDs( 1208: 0 );

    // Update the authorization token in the request header
    http_xproc(HTTP_POINT_ADDL_HEADER: %paddr(add_headers));

    // Call the REST API and post the JSON
    Clear result;
    rc = http_url_post_raw( url
    : %Addr(postJSON)
    : %Len(%TrimR(postJSON))
    : 1
    : %paddr(SaveData)
    : HTTP_TIMEOUT
    : HTTP_USERAGENT
    : 'application/x-www-form-urlencoded');
    if (rc <> 1);
    http_crash();
    endif;

    //----------------------------------------------------------
    // Update the Request Headers prior to calling the REST API
    //----------------------------------------------------------
    dcl-proc add_headers;

    dcl-pi *n like(headers);
    end-pi;

    dcl-s headers varChar(32767) inz;

    dcl-c CRLF x'0d25';
    dcl-s oAuthToken char(1024) inz;

    oAuthToken = getCodeValue(SYSTEM_KEY: 'OAUTH_TOKEN');

    headers = 'Content-Type: text/plain;charset=UTF-8' + CRLF
    + 'Authorization: Bearer ' + %Trim(oAuthToken) + CRLF;

    return headers;

    end-proc;

    ----------------------------------------------------------------------
    DEBUG LOG:
    ----------------------------------------------------------------------

    Protocol Used: TLS Version 1.2
    http_persist_post(): entered
    http_persist_req(POST) entered.
    http_long_ParseURL(): entered
    http_long_ParseURL(): entered
    do_oper(POST): entered
    There are 0 cookies in the cache
    POST /services/apexrest/ir-order-xxxxxxxxxxx-receiver/bulk HTTP/1.1
    Host: XXXXXXXXXXX.salesforce.com
    User-Agent: http-api/1.39
    Content-Type: application/x-www-form-urlencoded
    Content-Length: 48
    Authorization: Basic aXNlcmllcy5XXXXXXXXXXXXxMTlqaGth


    senddoc(): entered
    {"message":"first test order","order":"9876543"}
    recvresp(): entered
    HTTP/1.1 401 Unauthorized
    Date: Wed, 27 Nov 2019 15:48:33 GMT
    Strict-Transport-Security: max-age=31536002; includeSubDomains
    Public-Key-Pins-Report-Only: pin-sha256="9xxxxxxxsY="; pin-sha256="5kJvNEMwxxxxx1w="; pin-sha256="njN4rRG+22dNXAi+xxxxxg="; max-age=86400; includeSubDomains; report-uri="https://a.forcesslreports.com/hpkp-r...D2i0000000Mmdm";
    Expect-CT: max-age=86400, report-uri="https://a.forcesslreports.com/Expect...t/00D2ixxxxxxm"
    X-Robots-Tag: none
    Cache-Control: no-cache,must-revalidate,max-age=0,no-store,private
    Set-Cookie: BrowserId=Xb_B1hEtEeqhzzmOdWQ-Bw;Path=/;Domain=.salesforce.com;Expires=Sun, 26-Jan-2020 15:48:33 GMT;Max-Age=5184000
    Expires: Thu, 01 Jan 1970 00:00:00 GMT
    Content-Type: application/json;charset=UTF-8
    Transfer-Encoding: chunked


    SetError() #13: HTTP/1.1 401 Unauthorized
    recvresp(): end with 401
    recvdoc parms: chunked 0
    SetError() #36: This page requires a user-id & password
    http_close(): entered

Working...
X