TypeError: source.on is not a function in request request
Explanation of the problem
The following code snippet attempts to perform a POST request to an HTTP endpoint with an array of values using the request-promise
library. The request is sent with the formData
option, which expects an array of objects as its value.
return rp.post({
url: uri("collections/people"),
formData: [{a: 1}, {a: 2}],
});
However, the code throws a TypeError: source.on is not a function
error. The error message indicates that the issue is related to the creation of a DelayedStream
object in the delayed-stream
module. Specifically, the error occurs in the create
function of the DelayedStream
module.
The error traceback also shows that the issue could be caused by the CombinedStream
and FormData
modules. These modules are used by the request
library, which is used by request-promise
under the hood.
The code author suspects that the error might be related to passing an array to the formData
option of the rp.post()
function. The author is wondering whether arrays are allowed in the formData
option, and whether this could be the cause of the error.
Troubleshooting with the Lightrun Developer Observability Platform
Getting a sense of what’s actually happening inside a live application is a frustrating experience, one that relies mostly on querying and observing whatever logs were written during development.
Lightrun is a Developer Observability Platform, allowing developers to add telemetry to live applications in real-time, on-demand, and right from the IDE.
- Instantly add logs to, set metrics in, and take snapshots of live applications
- Insights delivered straight to your IDE or CLI
- Works where you do: dev, QA, staging, CI/CD, and production
Start for free today
Problem solution for TypeError: source.on is not a function in request request
To fix the TypeError: source.on is not a function
error that occurs when using the request-promise
library to send a POST request with an array of values, there are several things that could be done.
One potential cause of the error is passing an array to the formData
option of the rp.post()
function. The formData
option expects an object or an array of objects, where each object represents a key-value pair to be included in the request body. Therefore, the formData
option should be updated to pass an object or an array of objects with valid key-value pairs.
Another possible solution is to ensure that the form-data
module is installed and up-to-date. The form-data
module is used by the request
library to handle form data in requests. An outdated or missing form-data
module could cause issues with the request
library, which could in turn cause the TypeError: source.on is not a function
error.
Additionally, the issue could be related to the version of the request-promise
library being used. Upgrading to the latest version of the library may solve the issue, as newer versions may have bug fixes and improvements that address this specific error.
Lastly, it is also worth checking whether there are any other dependencies that could be causing conflicts or issues with the request-promise
library. Ensuring that all dependencies are up-to-date and compatible with each other could help resolve the issue.
Other popular problems with request request
Problem: SSL/TLS certificate verification failure
When making a request to an HTTPS endpoint with the request
library, you may receive an error indicating a failure to verify the SSL/TLS certificate of the server. This error occurs when the certificate presented by the server does not match or is not trusted by the client.
Solution:
To resolve this issue, you can disable SSL/TLS certificate verification for the request by setting the rejectUnauthorized
option to false
. However, this is not recommended in production environments, as it can leave your application vulnerable to man-in-the-middle attacks. A better solution is to use a trusted certificate or add the server’s certificate to the client’s trust store. Alternatively, you can use a package like https-proxy-agent
to forward requests through a proxy that handles the SSL/TLS encryption and certificate verification.
Problem: Handling redirects
By default, the request
library follows HTTP redirects, which can lead to unexpected behavior if the redirected location is not intended. This can also result in infinite redirects, which can cause the request to hang indefinitely.
Solution:
To control how the request
library handles redirects, you can set the followRedirect
option to false
. This will cause the library to return the redirect response instead of following the redirect. You can then inspect the response to determine whether the redirect is intended or not. If the redirect is intended, you can then make a new request to the redirect location with the same options as the original request.
Problem: Uploading files
When uploading files using the request
library, you may encounter issues with file encoding or formatting. This can result in corrupted or incomplete files being uploaded, or the file not being uploaded at all.
Solution:
To upload files with the request
library, you can use the form-data
module to create a FormData
object that represents the file data. You can then pass this object to the formData
option of the request
function. Alternatively, you can use the request
library’s multipart/form-data
encoding to directly send the file data in the request body. When sending files, it is important to specify the correct content type and encoding to ensure that the file is correctly interpreted by the server. Additionally, you should ensure that the file is read and uploaded in binary mode to avoid any encoding issues.
A brief introduction to request request
request
is a popular and versatile module in Node.js used for making HTTP requests to servers. It provides an easy-to-use API that abstracts away the complexities of making HTTP requests, while still offering advanced features for handling responses, managing cookies, and setting custom headers.
The request
module can be used to make various types of HTTP requests, such as GET, POST, PUT, DELETE, PATCH, and more. It supports both HTTP and HTTPS protocols, and can be used to interact with RESTful APIs or scrape web pages. Additionally, it offers several options for customizing the request, such as setting headers, cookies, and query parameters, as well as handling timeouts and redirects. The response from the server is passed back in a callback function, which can be used to handle the response data, errors, and status codes. Overall, request
is a flexible and powerful module for handling HTTP requests in Node.js applications.
Most popular use cases for request request
- Making HTTP requests:
request
can be used to make various types of HTTP requests to servers, such as GET, POST, PUT, DELETE, PATCH, and more. It supports both HTTP and HTTPS protocols and can be used to interact with RESTful APIs or scrape web pages. Here is an example of usingrequest
to make a GET request:
const request = require('request');
request('http://www.example.com', function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body);
}
});
- Customizing requests:
request
offers several options for customizing requests, such as setting headers, cookies, and query parameters, as well as handling timeouts and redirects. Here is an example of usingrequest
to make a POST request with custom headers and data:
const request = require('request');
const options = {
url: 'http://www.example.com/api',
headers: {
'User-Agent': 'request'
},
form: {
key: 'value'
}
};
request.post(options, function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body);
}
});
- Handling responses:
request
provides several ways to handle responses from servers, such as parsing JSON or HTML, streaming data, or handling errors and status codes. Here is an example of usingrequest
to stream a file from a server:
const request = require('request');
const fs = require('fs');
const url = 'http://www.example.com/file.mp3';
const file = fs.createWriteStream('file.mp3');
request.get(url)
.on('response', function(response) {
console.log(response.statusCode);
console.log(response.headers['content-type']);
})
.on('error', function(err) {
console.log(err);
})
.pipe(file);
These are just a few examples of what request
can be used for. With its powerful and flexible API, request
can be used for a wide variety of use cases in Node.js applications.
It’s Really not that Complicated.
You can actually understand what’s going on inside your live applications.