1

I have tried making the same post request using node-fetch and axios.

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

async function fetchTest(host, port, body) {
    const response = {
        successMessage: '',
        errorMessage: ''
    }

    try {
        const streamResponse = await fetch(`${host}:${port}`, {
            method: 'post',
            body: JSON.stringify(body)
        })
        const jsonResponse = await streamResponse.json()
        response.successMessage = jsonResponse;
    } catch (error) {
        response.errorMessage = error;
    }

    return response;
}

async function axiosTest(host, port, body) {
    const response = {
        successMessage: '',
        errorMessage: ''
    }

    try {
        const jsonResponse = await axios({
            method: 'post',
            url: `${host}:${port}`, 
            data: body
        })
        response.successMessage = jsonResponse;
    } catch (error) {
        response.errorMessage = error;
    }

    return response;
}

async function test() {
    console.log(await fetchTest('http://127.0.0.1', '8801', { type: 'request', cmd: 'devices' }));
    console.log(await axiosTest('http://127.0.0.1', '8801', { type: 'request', cmd: 'devices' }));

}

test();

The request made with node-fetch works nicely. The request made with axios returns (IP address redacted with ...) an error:

{ successMessage: '',
  errorMessage:
   { Error: connect ECONNREFUSED ...:80
       at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1161:14)
     errno: 'ECONNREFUSED',
     code: 'ECONNREFUSED',
     syscall: 'connect',
     address: '...',
     port: 80,
     config:
      { adapter: [Function: httpAdapter],
        transformRequest: [Object],
        transformResponse: [Object],
        timeout: 0,
        xsrfCookieName: 'XSRF-TOKEN',
        xsrfHeaderName: 'X-XSRF-TOKEN',
        maxContentLength: -1,
        validateStatus: [Function: validateStatus],
        headers: [Object],
        method: 'post',
        url: 'http://127.0.0.1:8801',
        data: '{"type":"request","cmd":"devices"}' },
     request:
      Writable {
        _writableState: [WritableState],
        writable: true,
        _events: [Object],
        _eventsCount: 2,
        _maxListeners: undefined,
        _options: [Object],
        _redirectCount: 1,
        _redirects: [],
        _requestBodyLength: 39,
        _requestBodyBuffers: [],
        _onNativeResponse: [Function],
        _currentRequest: [ClientRequest],
        _currentUrl: 'http://.../',
        _isRedirect: true },
     response: undefined } }

What might be the reason? Am I doing something wrong?

EDITED with more info requested in comment:

The axios request fails the same way if I comment out the node-fetch request.

The node-fetch request returns:

{
    cmd: 'devices',
    payload: { devices: [Array] },
    type: 'response' },
}

Since I wrap the result in a response object, the console.log looks like

{ 
    successMessage:
    { 
        cmd: 'devices',
        payload: { devices: [Array] },
        type: 'response' 
    },
    errorMessage: '' 
}
user1283776
  • 19,640
  • 49
  • 136
  • 276
  • Could you share your messages (successMessage and errorMessage) for the node fetch? Also, could you test only the `axiosTest` by commenting the `fetchTest` line? do you get the same error? – c-chavez Nov 16 '18 at 10:27
  • Also, have a look at [this answer in SO](https://stackoverflow.com/a/50326744/1042409) to see if it helps. – c-chavez Nov 16 '18 at 10:27
  • @c-chavez: I have edited the post with the info you asked for. I looked at the SO response you linked (thanks! very good info there), but I did not see anything I think can help with my case. – user1283776 Nov 16 '18 at 11:11

0 Answers0