ncsoft / Unreal.js

Unreal.js: Javascript runtime built for UnrealEngine

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

HTTP call with header options

AmitMitr opened this issue · comments

Hello,

[UE 4.27.2]

Trying to use the example HelloRest to call my API.

The following works.

function main() {
let actor = new TextRenderActor(GWorld,{X:100,Z:100},{Yaw:180})
actor.TextRender.SetHorizontalAlignment('EHTA_Center')
actor.TextRender.SetText('Hello Resting 3000')
let result = "";
let request = require('request')

let url = "https://api.ipify.org/?format=json"

request("GET",url ,result).then((result) => {
console.log('This is the result : ', JSON.stringify(result))
actor.TextRender.SetText(JSON.stringify(result))
}).catch((error) => {
console.log('This is the Error : ', JSON.stringify(error))
actor.TextRender.SetText(error)
})

}

And I get the Set Text as the IP.

However when my URL is a bit more trickier in the above function, as in

let url = "http://10.yy.XX.152:50000/sap/opu/odata/sap/ZGW3407_01_STOCK_SRV_02/ZmatstockSet(matnr='T3407-3',werks='1710')?$format=json";

and the server calls for authentication, so I got

var options = {
url: url,
method: 'GET', // Don't forget this line
headers: {
"DataServiceVersion": "2.0" ,
"Accept": "application/json" ,
"Content-Type": "application/json",
"Apikey":"BgBF6Dn2vuWpKjY4YxWfU8sjmfzldTdC",
"X-Requested-With": "X"
},
form: {
'username' : 'User',
'password' : 'password',
}
};

And then I call the request as follows

request(options ,result).then((result) => {
console.log('This is the result : ', JSON.stringify(result))
actor.TextRender.SetText(JSON.stringify(result))
}).catch((error) => {
console.log('This is the Error : ', JSON.stringify(error))
actor.TextRender.SetText(error)
})

I get Error Invalid JSON.

However, I can confirm that my response is correct when the above is called in the browser.

as follows :

{
"d": {
"__metadata": {
"id": "http://10.79.13.152:50000/sap/opu/odata/sap/ZGW3407_01_STOCK_SRV_02/ZmatstockSet(matnr='T3407-3',werks='1710')",
"uri": "http://10.79.13.152:50000/sap/opu/odata/sap/ZGW3407_01_STOCK_SRV_02/ZmatstockSet(matnr='T3407-3',werks='1710')",
"type": "ZGW3407_01_STOCK_SRV_02.Zmatstock"
},
"matnr": "T3407-3",
"werks": "1710",
"UNRESTRICTED_STCK": "7.000 "
}
}

Also, I am able to call the above using an xhr. open('GET' , URL) with set header options.

Not sure where I am going wrong in unreal.js.

Thanks