The Community Forums

Interact with an entire community of cPanel & WHM users!
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Unable to make REST calls to my CPanel plugin

Discussion in 'cPanel Developers' started by jwineman, Nov 19, 2015.

  1. jwineman

    jwineman Registered

    Joined:
    Nov 19, 2015
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    San Francisco
    cPanel Access Level:
    Reseller Owner
    So I have this javascript:
    Code:
    import fetch from 'isomorphic-fetch';
    
    export default function proxyReqeust(method, url, body) {
        var headers = new Headers();
        headers.append('host_key', '0c919912bc9e8fac8e24bf65f76ce34a');
    
        var myInit = { method: method,
            headers: headers,
            body: JSON.stringify(body)};
    
        //TODO: clean up this hack
        var myurl = document.URL.substr(0,document.URL.lastIndexOf('/'));
        myurl = myurl.replace("index.html/#","index.php");
        return fetch('
    https://cpanel.domain:2083/cpsess5843135714/frontend/paper_lantern/[...]/index.php
    ',myInit);
    When I try to make a POST request to that URL I get the Cpanel login page. If I hit it in my browser it shows up fine so I thought maybe I could GET it but GET requests failed too. At first I was passing the relative url which I assume failed cause I was missing the CPanel session but now I'm passing in the absolute URL with the CPanel session and it still fails. What am I doing wrong?
     
    #1 jwineman, Nov 19, 2015
    Last edited by a moderator: Nov 19, 2015
  2. jwineman

    jwineman Registered

    Joined:
    Nov 19, 2015
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    San Francisco
    cPanel Access Level:
    Reseller Owner
  3. jwineman

    jwineman Registered

    Joined:
    Nov 19, 2015
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    San Francisco
    cPanel Access Level:
    Reseller Owner
    Okay the issue was my request body was malformed. Sorry to waste your time :)

    I'm a little confused as to why CPanel didn't let it through but thats okay.
     
  4. cPanelMichael

    cPanelMichael Forums Analyst
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    30,854
    Likes Received:
    676
    Trophy Points:
    113
    cPanel Access Level:
    Root Administrator
    I am happy to see you were able to address the issue. Thank you for updating us with the outcome.
     
  5. jwineman

    jwineman Registered

    Joined:
    Nov 19, 2015
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    San Francisco
    cPanel Access Level:
    Reseller Owner
    Just kidding, request body was properly formed. Root cause was the cookie wasn't been set in the header and firefox automatically adds it in without telling me which is why my browser requests worked and code requests failed:

    Isomorphic-fetch specific fix is to add credentials: 'same-origin' to your init.
    How do I use window.fetch() with httpOnly cookies
     
Loading...

Share This Page