Michael P.
Michael P.

Reputation: 1413

Node.js PATCH request with If-Match header

This is a simple question for which I've not been able to find an answer.

In Node.js, as I'm forming a PATCH request, I'd like to set the if-match header to *. Is this how I'd do it? Would this work?

headers: {
    'if-match': '*'
}

Upvotes: 0

Views: 1320

Answers (1)

shaochuancs
shaochuancs

Reputation: 16246

Yes, it works.

Here is a simple example.

Client Node.js program:

const http = require('http');
const agent = new http.Agent();
let req = http.request({
  agent: agent,
  port: 3000,
  path: '/',
  method: 'PATCH',
  headers: {
    'if-match': '*'
  }
});
req.end();

Server Node.js program (could be other server-side technology as well):

const http = require('http');

let server = http.createServer(function(req, res) {
  console.log(req.url);
  console.log(req.method);
  console.log(req.headers);
  res.end();
});

server.listen(3000, function() { console.log("Server Listening on http://localhost:3000/"); });

The printed result in console is:

/
PATCH
{ 'if-match': '*',
  host: 'localhost:3000',
  connection: 'close',
  'content-length': '0' }

You can see that both PATCH and if-match header is received in server side.

Upvotes: 1

Related Questions