1

I am using node-fetch modeule along with javascript to work with API requests. Below is my code (just a sample GET operation)

var fetch = require('node-fetch');

const HttpsProxyAgent = require('https-proxy-agent');

    fetch('http://postalpincode.in/api/pincode/600042', {method: 'GET', agent: new HttpsProxyAgent('http://10.10.104.4:50683') })
    .then(resRaw=>{
    resRaw.toJson();
    })
    .then(resJson=>{
    console.log(resJson);
    })

But I am getting the below error. Please help me resolving this. I have installed node-fetch, https-proxy-agent modules.

FetchError: request to http://postalpincode.in/api/pincode/600042 failed, reason: socket hang up
at ClientRequest. (F:\johny\three projects\test-nodefetch\node_modules\node-fetch\lib\index.js:1345:11)
at emitOne (events.js:116:13)
at ClientRequest.emit (events.js:211:7)
at Socket.socketOnEnd (_http_client.js:423:9)
at emitNone (events.js:111:20)
at Socket.emit (events.js:208:7)
at endReadableNT (_stream_readable.js:1064:12)
at _combinedTickCallback (internal/process/next_tick.js:138:11)
at process._tickCallback (internal/process/next_tick.js:180:9)
(node:19424) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:19424) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
johny
  • 51
  • 1
  • 11
  • have you tried without the proxy agent - I can't see the point of a https proxy agent in this code to be frank - and what sort of server is running at `http://10.10.104.4:50683` ? – Jaromanda X Jul 25 '18 at 06:53
  • by the way, the rest of the code will fail anyway - because your first then doesn't return anything - and it's `.json()` not `.toJson()` – Jaromanda X Jul 25 '18 at 06:56
  • In my VM there is a proxy, so I use that modeule. Without proxt agent, I get the below error node testnoproxy.js (node:10936) UnhandledPromiseRejectionWarning: FetchError: request to http://postalpincode.in/api/pincode/600042 failed, reason: connect ETIMEDOUT 184.168.224.180:80 at ClientRequest. – johny Jul 25 '18 at 07:34
  • seems your proxy is the culprit – Jaromanda X Jul 25 '18 at 08:28
  • Is there anyway to resolve this? – johny Jul 25 '18 at 10:42
  • I said that's what it looks like - but *"In my VM there is a proxy"* isn't much to go by – Jaromanda X Jul 25 '18 at 11:02

0 Answers0