Is it possible to start a server in Zapier Code - javascript

We need to send an HTTP CODE = 200 with a body 'OK' in reply to a notification through Zapier.
Is it possible to use the following code in Zapier:
var http = require('http');
const server = http.createServer((req,res) => {
res.statusCode = 200;
res.end('OK');
}).listen(80);
It returns an error:
Error: You did not define `output`! Try `output = {id: 1, hello: "world"};`
And the reply doesn't work.

David here, from the Zapier Platform team.
To cut to the chase - though it might be possible to start an http server (there's no reason it wouldn't be, as far as I know), it's not going to do what it seems like you're hoping to do. Namely, you can't send a custom response to an incoming webhook. From the docs:
There is no way to customize the response to the request you send to the Catch Hook URL, as the response is sent before the Zap triggers and runs on the webhook request.
If you need behavior like that, I'd suggest running a webserver.
The specific Code step error you're seeing has to do with not defining output to the function. Something goes in and something must come out. You can customize the output based on the input and use that output, but something has to be returned from the function (even if it's just {}).

Related

error description from http error code - react.js/javascript

I have a Component in my react.js application, which display an error code.
Like this:
Now I'm displaying just a code, along with this what I want to to is - show the reason of this error too. Like this page contains a lot of information about error codes.
How can I do it ?
If I get any error, then I get the error code and use window.location="/bad_request/".concat(err_code);
In my ErrorPage component I've :
componentWillMount() {
this.state.errorCode = this.props.match.params.errorCode;
console.log(this.state.errorCode);
}
Rest is simple.
In common, HTTP response codes are being used for describing a general result of the response. For example, server should return one of 20x codes (200, 201, 202, ...) on some success operation or 40x (400, 401, 401, ...) on client error etc.
That said, code itself won't describe an actual request result in details so you have to do consider an error code only like general reference point.
Answering to you question, I'd suggest you need to take a following steps:
1. Review your server response structure to make it include an actual error message in response body.
2. Review your component structure to retrieve error message from response body to display it (or perform any other required actions)
Basically this error indicating that server didn't receiving complete req. in its time period So it is giving this error So you use increase server timeout as well as you can check with sending less data on server. For more info:
https://airbrake.io/blog/http-errors/408-request-timeout
I wrote this to my java-script file :
let map = new Map();
map.set(400,'Bad Request');
map.set(401,'Unauthorized');
...
When I get redirected to this page, I just get what was the description of the error.
Now I have entered all the data manually, I was just asking if there was any automated way to log these.

How to get error HTTP status code in react admin

I am using react-admin framework and I am trying to get error status code (404,500), which I would be saving to some variable for later usage. For example, when I try to create an user that has same e-mail address as already created user, the server wont allow this operation and the request fails with status code 500.
I need that status code to save to variable and work with it later.
Does react-admin offer this?
So far I have tried this simple code in my custom DataProvider, but when logging it to console, it returns undefined.
const status = params.message.status;
I wasn't able to find any other solution that I would fully understand.
Any ideas how to solve this?
Thank you in advance.
Basically this is what are you looking for:
https://developer.mozilla.org/en-US/docs/Web/API/Response/status
First of all, you actually need to request some data from the server in order to get the response.status code.
The most simple example would be this based on the link I listed before:
var myRequest = new Request(options.body); //depends on what do you want to request
fetch(myRequest).then(function (response) { //gets the response from server
console.log(response.status, 'status'); // this returns the status value
});
This should return the response.status code.

Stop Node.js upon GET (or POST) request to given URL

I'm trying to write a simple Node.js http server which stops and exits its process when a given url is accessed: in this case, /stop. This is my program so far:
var http = require('http');
var server = http.createServer(function(request,response) {
response.end(function(){
if (request.url === '/stop') server.close();
});
});
server.listen(12345);
What I expect to happen is that the callback is called when the response finishes sending, and the server closes if the URL is /stop. However, all this ends up doing is it shows a blank page (which I expected) and does nothing else.
On the other hand, this program almost works if I omit response.end():
var http = require('http');
var server = http.createServer(function(request,response) {
if (request.url === '/stop') server.close();
});
server.listen(12345);
It does manage to stop the server, but not until I terminate the GET request by closing the browser window.
How can I get the server to stop when I GET a certain URL without having to close the browser window? (It would be nice if I could send a "Server stopped" message to the browser, too.)
NOTE: I know that I don't actually have the code to differentiate between GET and POST requests, but I would like to be able to handle both, and I'm assuming the answer isn't different depending upon the type of request.

Node request() not being sent

I'm using the npm request library and am running into an issue where the request is never sent if I call express's res.send() after calling request. I realize the request callback won't fire if I close the connection, but I'm not even seeing the request being sent in the first place.
This code is being executed on RunKit (formerly TonicDev), an online code editor that allows code execution via endpoints. I'm not seeing this issue on my local machine, so it seems like it may have to do with RunKit. Anyone have any ideas as to what's going on here or how I might work around this?
You can execute the code yourself by going to:
https://runkit.com/gragland/58056bc6e9d9ed00130c84d5 and clicking the endpoint link at the top.
// Helper to return a RunKit compatible express app (runkit.com/tonic/express-endpoint)
var tonicExpress = require("#runkit/tonic/express-endpoint/1.0.0")
// Provide the exports object to the tonicExpress helper
var app = tonicExpress(module.exports)
var request = require('request')
app.get("/", function(req, res){
var request_number = 9
request({
// To see if request is sent go to: https://requestb.in/1coqbqn1?inspect
url: 'http://requestb.in/1coqbqn1',
method: 'POST',
json: {
request_number: request_number,
message: 'hello'
}
})
// The line below has to be commented out for the above request to be sent
// I don't care about the request callback() firing, I just want the request to be sent
res.send('Done')
})

Is there a way to know a Twilio call failed without calling the REST API?

I'm using Twilio.Device in an Angular app and I need to display a message if the call failed due to an invalid number. I know that you can call Twilio's REST API to get the call status, but is there a way to get the status without making that extra call?
For example, I was hoping that the connection object that you get back in the disconnect handler would give you the status, like this:
Twilio.Device.disconnect(function(connection) {
if (connection.status === 'failed') {
// display error message
}
});
However, that's either not possible or I'm not looking in the right place.
Another idea I had is when I set debug to true, I can see this helpful log when making a call with an invalid number:
[Connection] Received HANGUP from gateway
[Connection] Disconnecting...
But is there any way to access that HANGUP event?
Thanks in advance!
I'm not sure exactly which you'd need, but in the accept or connect methods, try listening for the 'hangup' or 'error' events which are emitted by the Connection class:
Twilio.Device.connect(function(connection) {
connection.on('hangup', function (err) {
console.log(err)
})
})
Twilio developer evangelist here.
You can actually get all the details that you would normally need the Twilio REST API for on the Twilio.Connection object. Just take a look at the parameters attribute, it contains all the normal Twilio voice request parameters including CallStatus.

Categories