Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 671
ysh1685 Avatar asked Mar 29 '26 20:03

ysh1685


1 Answers

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();
like image 79
Trent Avatar answered Apr 01 '26 07:04

Trent



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!