Forum Discussion

isxtn's avatar
isxtn
Advisor
11 months ago

Javascript fetch vs Freemarker http.client.request issues

I'm having issues with permissions with a Freemarker http.client.reques that works fine as a Javascript fetch.

Javascript (returns result as expected):

 

    let myHeaders = new Headers();
            myHeaders.append( "authorization", "APIKey,apikey_version=2.0" );
            myHeaders.append( "content-type", "application/json" );
            myHeaders.append( "x-lc-community-host", "community.stage.site" );
            let graphql = JSON.stringify({
  query: "\nfragment questionFields on Question {\n  id\n  user{\n      id\n      displayName\n      __typename\n  }\n}\n\n\n\nquery post($id: ID!, $lookupColumn: IdType, $lithium: Boolean) {\n  post(id: $id, lookupColumn: $lookupColumn, lithium: $lithium) {\n    ...questionFields\n    __typename\n  }\n}\n",
  variables: {"id":"12345","lookupColumn":"id","preprodStubbing":false}
} );                                  
            let requestOptions = {
                method  : 'POST',
                headers : myHeaders,
                body    : graphql,
                redirect: 'follow'
            };

            fetch( "apisite.com/graphql", requestOptions )
                .then( response => response.text() )
                .then( result => {
                  console.log(result);
                 } )
                .catch( error => console.log( 'error', error ) );

 

 

But converting that to an endpoint (controller), I get an error, 

{'error' : "http request not allowed: http client not allowed: hostname-not-in-allowed-domains-list"}

 

Any ideas why these should result in different results? Am I missing something on the http.client.request version?

Freemarker code:

 

<#assign articleID = http.request.parameters.name.get("articleID",'1234') />

<#assign graphQLCall = '{
  query: "\nfragment questionFields on Question {\n  id\n  user{\n      id\n      displayName\n      __typename\n  }\n}\n\n\n\nquery post($id: ID!, $lookupColumn: IdType, $lithium: Boolean) {\n  post(id: $id, lookupColumn: $lookupColumn, lithium: $lithium) {\n    ...questionFields\n    __typename\n  }\n}\n",
  variables: {"id":"1234","lookupColumn":"id","preprodStubbing":false}
}' />


    <#assign
    apiResponse = http.client.request("https","apisite.com","graphql")
    .header("authorization", "APIKey,apikey_version=2.0" )
    .header("content-type", "application/json" )
    .header("x-lc-community-host", "community.stage.site")
    .body(graphQLCall,"application/json")
    />

<#assign apiResponse = apiResponse.post() />

<#if apiResponse.hasError??>
  {'error' : "${apiResponse.error.message}"}
<#else>
    ${apiResponse.content!"NO content"}
</#if>

 

5 Replies

  • Hi isxtn 

    There is an "Allowed domain names" setting in admin. I think you just need to add your domain name there "apisite.com" in your example. It's found in Community Admin, under System > HTTP Client

     

     

  • isxtn's avatar
    isxtn
    Advisor
    11 months ago

    Thanks Akenefick Unfortunately that didn't do it. I suspect I may have something wrong in the syntax of the http.client.request code but I'm not sure what it is. Documentation is thin. As usual.

  • ThomasS's avatar
    ThomasS
    Khoros Staff
    11 months ago

    Ian can you try again by only using domain in the setting (i.e. just intuit.com)?

  • Nope. Thanks, anyway, friend. I've opened a support case, so Khoros will give me an answer sometime in the next six months on this.