ysh1685
ysh1685

Reputation: 55

can't make this specific http patch request in node.js

Here is how I do the request in curl:

curl -v --request PATCH -H "Authorization: token TOKEN-VALUE-FROM-PREVIOUS-CALL" -d '{"description": "updated gist","public": true,"files": {"file1.txt": {"content": "String file contents are now updated"}}}' https://api.github.com/gists/GIST-ID-FORM-PREVIOUS-CALL

I have tried a few node libraries to make that "patch" request but no success. I can also do the same thing in client side with jQuery.ajax but can't get it working on the server side. Thanks in advance.

Upvotes: 3

Views: 5227

Answers (1)

Trent
Trent

Reputation: 4306

Using the native HTTP request function in Node.js, you can make your requests as follows -

var qs = require("querystring");
var http = require("https");

var options = {
  "method": "PATCH",
  "hostname": "api.github.com",
  "port": null,
  "path": "/gists/GIST-ID-FORM-PREVIOUS-CALL",
  "headers": {
    "authorization": "token TOKEN-VALUE-FROM-PREVIOUS-CALL",
    "cache-control": "no-cache",
    "content-type": "application/x-www-form-urlencoded"
  }
};

var req = http.request(options, function (res) {
  var chunks = [];

  res.on("data", function (chunk) {
    chunks.push(chunk);
  });

  res.on("end", function () {
    var body = Buffer.concat(chunks);
    console.log(body.toString());
  });
});

req.write(qs.stringify(
{
  "description": "updated gist",
  "public": "true",
  "files": {
    "file1.txt": {
      "content": "String file contents are now updated"
    }
  }
}));

req.end();

Upvotes: 4

Related Questions