isxtn
11 months agoAdvisor
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>