To install the package use these commands:
npm install node-fetch#2.6.1
npm install --save-dev #types/node-fetch#2.x
I am trying to get a Json file using the following code:
import fetch from "node-fetch";
async function getjson(){
const response = await fetch('https://www.carrefour.es/electronica/television/ofertas-en-televisores-y-accesorios/cat4167531/c', {method: 'GET',
headers:{
Accept: 'application/json'
}
}).catch(e => {console.error('request failed:' , e)});
const result = ( response) ? await response.json(): null;
console.log('start');
console.log(result);
}
getjson();
but instead of returning the expected json file, it returns the following error
reason: Parse Error: Invalid header value char
at ClientRequest.<anonymous> (/home/shengxing/Documents/proyectos/bot/node_modules/node-fetch/lib/index.js:1461:11)
at ClientRequest.emit (events.js:314:20)
at TLSSocket.socketOnData (_http_client.js:481:9)
at TLSSocket.emit (events.js:314:20)
at addChunk (_stream_readable.js:297:12)
at readableAddChunk (_stream_readable.js:272:9)
at TLSSocket.Readable.push (_stream_readable.js:213:10)
at TLSWrap.onStreamRead (internal/stream_base_commons.js:188:23) {
type: 'system',
errno: 'HPE_INVALID_HEADER_TOKEN',
code: 'HPE_INVALID_HEADER_TOKEN'
}
When I use Postman, it returns the expected json file, what was I doing wrong?
Can anyone shed some light on this question please?
Related
I'm using Angular's HttpClient to perform a POST request to my NodeJS server, like so:
createData(data:any):Observable<any> {
// verifying the content type is need to ensure the JSON object is sent as
// JSON object to NodeJS server
const options = {
headers: new HttpHeaders({
'Content-Type': 'application/json',
})
};
// This still throws an HTTPResponse error, -
// SyntaxError: Unexpected token A in JSON at position 0 at JSON.parse
return this._http.post(`${this.apiUrl}`, data, options);
}
And my server's POST function is set up like so:
router.post('/', async (req,res) => {
const body = req.body;
await database.execute(`
INSERT INTO Post (
title,
body,
date_added
) VALUES (
#title,
#body,
NOW()
)
`, {
title: body.title,
body: body.body,
})
res.end('Added post')
})
When createData is called, the POST method is performed (I checked the Network panel in dev tools, the response "Added post" is returned from the server, and my json object is sent as the payload), but the console still returns this HTTPErrorResponse:
SyntaxError: Unexpected token A in JSON at position 0 at JSON.parse () at XMLHttpRequest.onLoad (http://localhost:4200/vendor.js:7508:51) at ZoneDelegate.invokeTask (ht..
What could be the cause of this error, if the server function has returned successfully?
The server could return successfully, however the callback function CreateData was expecting a JSON object to be returned from the server. Because the string returned is not JSON, an HTTPErrorResponse was thrown. The solution is to change the string returned from my server to a JSON object, like so:
router.post('/', async (req,res) => {
const body = req.body;
await database.execute(`
INSERT INTO Post (
title,
body,
date_added
) VALUES (
#title,
#body,
NOW()
)
`, {
title: body.title,
body: body.body,
})
res.end({}) // returns successfully, no error
})
Trying to make request to Google.com using code from npm require website:
var request = require('request');
request('http://www.google.com', function (error, response, body) {
console.log('error:', error); // Print the error if one occurred
console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
console.log('body:', body); // Print the HTML for the Google homepage.
}); , but receiving this error:
error: { Error: getaddrinfo ENOTFOUND www.google.com www.google.com:80
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:56:26)
errno: 'ENOTFOUND',
code: 'ENOTFOUND',
syscall: 'getaddrinfo',
hostname: 'www.google.com',
host: 'www.google.com',
port: 80 }
statusCode: undefined
body: undefined.
I've installed request. How can I fix this?
I am supposed to send data from an app to the server and the post method from that app is made using content type as application/json but it is plain text. I cannot update the app to change this header now. The current app is working as the data reaches PHP directly and PHP doesn't parse the incoming data which is specified as json.
import express from 'express'
var http = require('http')
const redirectionRoutes = express.Router()
redirectionRoutes.use(function(req, res, next) {
req.rawBody = ''
req.headers['content-type'] = 'text/plain'
req.on('data', function(chunk) {
req.rawBody += chunk
})
req.on('end', function() {
next()
})
})
redirectionRoutes.post(/^\/update_services\/.*$/, function(request, response) {
var data = request.rawBody
var dataLength = data.length
var options = {
hostname: 'localhost',
port: 80,
path: request.path,
method: 'POST',
json: false,
headers: {
'Content-Type': 'text/plain',
'Content-Length': dataLength
}
}
var buffer = ''
var req = http.request(options, function(res) {
res.on('data', function(chunk) {
buffer += chunk
})
res.on('end', function() {
response.send(buffer)
})
})
req.write(data)
req.end()
})
But in nodejs(my application), as the content type is specified as json, the body parser is parsing the data and as it's not json, I am getting an error:
SyntaxError: Unexpected token # in JSON at position 0
at JSON.parse (<anonymous>)
at createStrictSyntaxError (../node_modules/body-parser/lib/types/json.js:157:10)
at parse (../node_modules/body-parser/lib/types/json.js:83:15)
at /Users/../node_modules/body-parser/lib/read.js:116:18
at invokeCallback (/Users/../node_modules/body-parser/node_modules/raw-body/index.js:224:16)
at done (/Users/../node_modules/body-parser/node_modules/raw-body/index.js:213:7)
at IncomingMessage.onEnd (/Users/../node_modules/body-parser/node_modules/raw-body/index.js:273:7)
at emitNone (events.js:105:13)
at IncomingMessage.emit (events.js:207:7)
at endReadableNT (_stream_readable.js:1047:12)
Is there a way in nodejs/body parser to not to parse this incoming json and let is get into the function as plain text.
It is solved!!
I am exporting this module at the end of the app code along with other pages routers. So, the body-parser being called in previous libraries are being called if I didn't use in this particular router.
I am planning to translate the curl API call below to node.js though I am still getting an error when doing a POST.
curl -X POST --user user1#customer1:secret http://demo.test.com/controller/actions/38 -F file=#ExportActions.json
Below is initial code using the request - npm module, but the API call still fails.
var requestdata = fs.readFileSync('./ExportActions.json').toString();
var request = require('request');
request.post({
url: 'https://demo.test.com/controller/actions/38',
auth: {
'user': 'user1#customer1',
'pass': 'secret'
},
body: requestdata
}, function(error, response, body){
console.log(body);
});
I am getting the error below every time I run the script:
Could not import Actions: org.apache.commons.fileupload.FileUploadException: the request was rejected because no multipart boundary was found
Are there any workarounds when posting a JSON file using the npm request module?
Thanks!
You are trying to upload to file by posting it to the body. It looks like the server wants a multi-part upload instead. This might work better
var req = require('request');
request.post({
url: 'https://demo.test.com/controller/actions/38',
auth: {
'user': 'user1#customer1',
'pass': 'secret'
},
}, function(error, response, body){
console.log(body);
});
var form = req.form()
form.append('file', fs.readFileSync('./ExportActions.json'));
You can use Postman client to check this api instead of curl.
I'm writing a node app with React, using node-postgres and superagent for backend calls.
Let's say I'm making a GET request and using the JSON it returns to fill a table of students. My API looks like this:
import pg from 'pg';
import Router from 'express';
let router = new Router();
let conString = "postgres://user:pass#localhost/db_name";
router.get('/getStudents', function(req, res) {
var results = [];
pg.connect(conString, function(err, client, done) {
if (err) {
done();
console.log(err);
return res.status(500).json({success: false, data: err});
}
var query = client.query('SELECT first_name, last_name, email FROM students');
query.on('row', function(row) {
results.push(row);
});
query.on('end', function() {
done();
return res.json(results);
});
});
});
On page load, this is called from the store to set a students array. It seems like something is going wrong here:
var request = require('super agent');
function getStudents() {
request
.get('/api/getStudents')
.set('Accept', 'application/json')
.end(function(err, res) {
if (err) {
console.log("There's been an error: getting students.");
console.log(err);
} else {
return res;
}
});
}
If I curl localhost:3000/api/getStudents, I get the JSON response I expect.
However, when I call this on page load, I get an ECONNREFUSED error:
Error: connect ECONNREFUSED 127.0.0.1:80]
code: 'ECONNREFUSED',
errno: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 80,
response: undefined
Not sure why I'm getting an error on the HTTP port. This is my first time using node-postgres, superagent, and React so any help is appreciated.
Edit: Forgot to mention that I'm able to make POST requests and insert items into the database without any errors. This error only occurs when I'm attempting a GET request.
Try this (inserting the full path url) in the get method:
request
.get('http://localhost:3000/api/getStudents')
.set('Accept', 'application/json')
.end(function(err, res) {
if (err) {
console.log("There's been an error: getting students.");
console.log(err);
} else {
return res;
}
});
Check out the documentation for CORS for an example of using absolute urls:
https://visionmedia.github.io/superagent/#cors
The error will also occur if you don't have the protocol in your request URL.
Instead
request.get('www.myexample.com/api/getStudents')
do
request.get('https://www.myexample.com/api/getStudents')
^^^^^^^^