GitHub Webhook parse request object in Node.js - javascript

I would like to design and code a simple LaTeX CI environment. I need to parse request object that GitHub Webhook sends through HTTP POST for push and pull request events in Express (Node.js), but I can't find where it is located exactly.
GitHub API does not say where payload resides inside request object.
Also, how should JSON response be made? What HTTP code or JSON content should it have to communicate success or failure?
E.g.:
var app = express();
app.post('/build', function(req, res) {
// request parsing
// body
// send response
});
Thanks in advance for response.

Related

How can I get the raw HTTP message body using the request library in Node.js?

The npm-request library allows me to construct HTTP requests using a nice JSON-style syntax, like this.
request.post(
{
url: 'https://my.own.service/api/route',
formData: {
firstName: 'John',
lastName: 'Smith'
}
},
(err, response, body) => {
console.log(body)
}
);
But for troubleshooting, I really need to see the HTTP message body of the request as it would appear on the wire. Ideally I'm looking for a raw bytes representation with a Node.js Buffer object. It seems easy to get this for the response, but not the request. I'm particularly interested in multipart/form-data.
I've looked through the documentation and GitHub issues and can't figure it out.
Simplest way to do this is to start a netcat server on any port:
$ nc -l -p 8080
and change the URL to localhost in your code:
https://localhost:8080/v1beta1/text:synthesize?key=API_KEY
Now, any requests made will print the entire, raw HTTP message sent to the localhost server.
Obviously, you won't be able to see the response, but the entire raw request data will be available for you to inspect in the terminal you have netcat running
I figured out how to dump the HTTP message body with Request. In both cases, I'm just copying the same approach that request uses internally.
Multipart Form Uploads
req._form.pipe(process.stdout);
URL-encoded Forms
console.log(req.body);
You could try #jfriend00 suggestion an use a network sniffer like wireshark but as you're fetching an https URL this might not be the easiest route as it requires some setup to intercept TLS connections.
So maybe it would be enough turning on debug mode for the request module itself, you can do that by simply setting require('request').debug = true. As a third option you could go with the dedicated debug module for request here which allows you to view request and response headers and bodies.
I can think of a number of ways to see the bytes of the request:
Turn on debugging in the request module. There are multiple ways to do that documented here including setting NODE_DEBUG=request or require('request').debug = true or using the request-debug module.
Use a network sniffer to see what's actually being sent over the socket, independent of your node.js code.
Create your own dummy http server that does nothing but log the exact incoming request and send your same request to that dummy server so it can log it for you.
Create or use a proxy (like nginx) that can dump the exact incoming request before forwarding it on to its final destination and send the request to the proxy.
Step through the sending of the request in the debugger to see exactly what it is writing to the socket (this may be time consuming, particularly with async callbacks, but will eventually work).
you could use a nodejs server capable of logging the raw request/response string , then direct your request to that server
i gave an example using both http and https server - no dependencies
nodejs getting raw https request and response

Why does my http request in Cloud9 always return an error?

I am currently developing a Cloud9 plugin, where I need to make a GET request to a remote web server. The following code is what I use to perform the request:
var http = imports.http;
http.request(url, {}, function(err, data, res) {
if(err) {
alert(err);
}
}
When I perform the same GET request in a web browser, valid JSON is returned. However, using this code, I always receive an Error object, which tells me that the number of loaded bytes is 0. What is going wrong?
The answer I received from the c9 community is the following: The reason is an unpermitted cross origin request (the URL points to a java servlet running on the same host inside a jetty server). The solution is therefore to include an Access-Control-Allow-Origin Header in the servlet's Response.

Client-side js to fetch node.js API calls

I am pretty new to node.js but I am using it to fetch data from an API. I want to return the response data to my client side js to be manipulated and injected into an html page. Pretty basic idea there, now do I make an HTTP request from the client side to my node.js to retrieve the JSON response text? How exactly do the client and server side communicate with eachother in this case?
factual.get(/t/places-us, {filters:{"locality":"seattle"}, {category_ids:{"$includes_any":[48, 50, 52]}}}, function (error, res) {
console.log(res.data);
});
You can use a framework like express.
Add a route to handle your client request.
Then build a response using your API response.

How do I set a route for a json response in express?

I am trying to follow this Angular tutorial for handling form submissions with promises. I am running my server on node and I'm using express to handle routes. When the form is submitted and I get to the line
var $promise = $http.jsonp('response.json', config)
My application responds by trying to find a route for response.json and then redirects to a 404 page. I then get an Uncaught syntax error because it's trying to parse my jade template as the response.json.
Is the best way to resolve this to define a route for the json response? Or is there something else I'm missing?
Sending JSON data to your server is just a normal request on a particular path with the data sent as JSON. If the request is a GET, then the data is URL encoded. If the request is a POST, then the data is sent encoded into the body. In either case, the body-parser module will parse it for you.
Here's a simple example for a get request to "/response.json":
var express = require("express");
var app = express();
var bodyParser = require('body-parser');
app.get('/response.json', bodyParser.json(), function (req, res) {
if (!req.body) return res.sendStatus(400);
// req.body is the parsed JSON that was sent with this request
// process it here
});
app.listen(80);
There are several different ways to use the body-parser module. You can see several other examples here: How do I consume the JSON POST data in an Express application.
And, the client code would be:
var $promise = $http.jsonp('/response.json', config)

Creating http echo server in node.js

What I need to do is simple node.js http echo server. However, servers that I found on node website and on some other places either don't work how I would like or I don't understand how they work.
I used this simple echo server and on my client side (written in java) I don't get the results I would want to.
var http = require('http');
http.createServer(function(request,response){
response.writeHead(200);
request.pipe(response);
}).listen(8080);
I would like my server to make a response consisting of response header and response body where I would have the whole http request that my client sent and got back. Client side is measuring time from sending a request and getting a whole response back. Client is also writing a response body to a file.
When I try using something like
response.write(request.url);
I get the request.url in the response body and it works but I would like to have the whole http request inside.
Any help would be appreciated.
use:
response.write(JSON.stringify(request.headers))
Add response.end() when done:
http.createServer(function(request,response){
response.writeHead(200)
request.pipe(response)
response.end()
}).listen(8080)
well you are trying to pipe the request to the response which doesn't make sense and doesn't work. I'd advice you to use the connect or express module. Then you can do this; response.status(200).send("Very Awesome").end().

Categories