The following code is an extract from Postman, where I am getting a response. For some reason the NodeJS version doesn't seem to be running and gives a "Socket hang up" error, where as the python version runs well. Both codes are pasted below:
const obj = {
asset: [
{
age: "33",
existingDisease: false,
gender: "MALE",
id: "883a8cb5446f4d6780db2e59bdf4ee35",
proposerRelationShip: "SELF",
used: false,
},
],
cover: [],
pinCode: "122001",
quoteId: "7637d7ff982145569fbfa604e3b74485",
sumInsured: "700000",
term: { unit: "YEAR", value: "1" },
};
const strObj = JSON.stringify(obj);
var options = {
method: "POST",
url: "https://sa.navi.com/v3/premium?paymentMode=SUBSCRIPTION",
headers: {
appversion: "2.2.4",
appversioncode: "92",
osversion: "Android_11",
deviceid: "3e44626c3bb37d4c",
defaultlocale: "en_US",
"x-session-token": "d635eb15-10aa-4315-9122-6ed3627dc1b8",
"x-click-stream-data":
'{"app":{"name":"Navi","version":"92","version_name":"2.2.4"},"device":{"device_id":"3e44626c3bb37d4c","advertising_id":"706649fa-50f0-410d-9c17-3085201808e2","manufacturer":"Google","model":"sdk_gphone_x86","os":"Android","os_version":"30"},"network":{"carrier":"Android","type":"Wifi"},"location":{"latitude":"37.4219983","longitude":"-122.084"},"user":{}}',
"x-target": "GI",
source: "APK",
"content-type": "application/json; charset=utf-8",
"content-length": "419",
"accept-encoding": "gzip",
"user-agent": "okhttp/4.9.0",
},
body: strObj,
};
console.log("Going to trigger API");
console.log(options);
request(options, function (error, response) {
if (error) throw new Error(error);
console.log(response.body);
});
Gives error:
Error: Error: socket hang up
While the same code in Python works:
url = "https://sa.navi.com/v3/premium?paymentMode=SUBSCRIPTION"
payload = "{\"asset\":[{\"age\":\"33\",\"existingDisease\":false,\"gender\":\"MALE\",\"id\":\"883a8cb5446f4d6780db2e59bdf4ee35\",\"proposerRelationShip\":\"SELF\",\"used\":false}],\"cover\":[],\"pinCode\":\"122001\",\"quoteId\":\"7637d7ff982145569fbfa604e3b74485\",\"sumInsured\":\"700000\",\"term\":{\"unit\":\"YEAR\",\"value\":\"1\"}}"
headers = {
'appversion': '2.2.4',
'appversioncode': '92',
'osversion': 'Android_11',
'deviceid': '3e44626c3bb37d4c',
'defaultlocale': 'en_US',
'x-session-token': 'd635eb15-10aa-4315-9122-6ed3627dc1b8',
'x-click-stream-data': '{"app":{"name":"Navi","version":"92","version_name":"2.2.4"},"device":{"device_id":"3e44626c3bb37d4c","advertising_id":"706649fa-50f0-410d-9c17-3085201808e2","manufacturer":"Google","model":"sdk_gphone_x86","os":"Android","os_version":"30"},"network":{"carrier":"Android","type":"Wifi"},"location":{"latitude":"37.4219983","longitude":"-122.084"},"user":{}}',
'x-target': 'GI',
'source': 'APK',
'content-type': 'application/json; charset=UTF-8',
'content-length': '419',
'accept-encoding': 'gzip',
'user-agent': 'okhttp/4.9.0'
}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)
CodePudding user response:
Your content-length is wrong (you're passing 419, when the actual length of your payload JSON is 279). This probably means the receiving server is still waiting for the rest of the content to arrive, it never arrives so eventually the server times out and hangs up on you (thus why you get a hang up error).
And, lo and behold, when I just remove the content-length header and let the request library compute it automatically, the request starts working for me.
As for the python implementation, one guess is that it is overriding the wrong content-length and fixing it for you.
FYI, the request() library has been deprecated and it is not recommended that people write new code with it as it will not be advanced with new features in the future. A list of suggested alternatives (that all support promises) is here. My favorite in that list is got() which also supports most of the same options as the request() library, but is promise-based at is core and has a number of API improvements that make it easier to use (in my opinion).
And, indeed your code works just fine with the got() library (after removing the incorrect content-length header).
const got = require('got');
const obj = {
asset: [{
age: "33",
existingDisease: false,
gender: "MALE",
id: "883a8cb5446f4d6780db2e59bdf4ee35",
proposerRelationShip: "SELF",
used: false,
}, ],
cover: [],
pinCode: "122001",
quoteId: "7637d7ff982145569fbfa604e3b74485",
sumInsured: "700000",
term: { unit: "YEAR", value: "1" },
};
const strObj = JSON.stringify(obj);
var options = {
method: "POST",
url: "https://sa.navi.com/v3/premium?paymentMode=SUBSCRIPTION",
headers: {
appversion: "2.2.4",
appversioncode: "92",
osversion: "Android_11",
deviceid: "3e44626c3bb37d4c",
defaultlocale: "en_US",
"x-session-token": "d635eb15-10aa-4315-9122-6ed3627dc1b8",
"x-click-stream-data": '{"app":{"name":"Navi","version":"92","version_name":"2.2.4"},"device":{"device_id":"3e44626c3bb37d4c","advertising_id":"706649fa-50f0-410d-9c17-3085201808e2","manufacturer":"Google","model":"sdk_gphone_x86","os":"Android","os_version":"30"},"network":{"carrier":"Android","type":"Wifi"},"location":{"latitude":"37.4219983","longitude":"-122.084"},"user":{}}',
"x-target": "GI",
source: "APK",
"content-type": "application/json; charset=utf-8",
"accept-encoding": "gzip",
"user-agent": "okhttp/4.9.0",
},
body: strObj,
};
console.log("Going to trigger API");
got(options).then(response => {
console.log(response.body);
}).catch(e => {
console.log(e);
});
