0

I want to send multiple files to Github repository via nodejs. Tried several approaches and end up using node-rest-client module. Tried below code send a sample file to repository called 'metadata'. But after post I am getting error message "Request forbidden by administrative rules. Please make sure your request has a User-Agent header"...please let me know if anyone faced this error before and get rid of it.

convertval = "somedata";
var dataObj = {
    "message": "my commit message",
    "committer": {
      "name": "Scott Chacon",
      "email": "ravindra.devagiri@gmail.com"
    },
    "content": "bXkgbmV3IGZpbGUgY29udGVudHM="
  }
debugger;
var Client = require('node-rest-client').Client;
var client = new Client()            
var args = {
    data: dataObj,
    headers: { 'Content-Type': 'application/json' },
};
client.post("https://api.github.com/repos/metadata/contents", args, function (data, response) {
    console.log("file send: True : " + data);
});
virgiliogm
  • 946
  • 7
  • 15
  • "Please make sure your request has a User-Agent header" - can you try adding a User-Agent header to your request? I'm surprised node-rest-client doesn't have a default one though. – Rup Jun 04 '19 at 09:29
  • Your URL is broken: there's no github.com/metadata/contents. Try something that does exist, e.g. https://api.github.com/repos/octokit/rest.js or https://api.github.com/repos/octokit/rest.js/contents – Rup Jun 04 '19 at 11:08

2 Answers2

0

First of all, you need to define 'User-Agent' with value 'request' in your request header. Refer to this link.

Second, endpoint you are trying to call might require authentication. Generate a personal token from here, add that token in your request header, 'Authorization': 'token '.

If you're using Git extensively in your code, I suggest you to use this - Nodegit.

Edit: I don't think sending multiple files in a single request is possible in 'Contents' endpoints group (link).

You can checkout Git Data API (as discussed here).

  • tried by adding user-agent and Authorization token, but I am getting 404 error – ravindra devagiri Jun 04 '19 at 09:59
  • 1
    Please read link after edit, https://developer.github.com/v3/repos/contents/ 404 means that either API endpoint you are trying does not exist, your API path parameters are wrong or repository does not exist. – Ajay Kumar Pradhan Jun 04 '19 at 10:05
0

According to the REST API:

All API requests MUST include a valid User-Agent header. Requests with no User-Agent header will be rejected.

joran
  • 2,815
  • 16
  • 18
  • Added User-Agent and headers properly `headers: { 'Content-Type': 'application/json', 'Authorization': '8566e4717dbcfebf05c857804a931732a9541283' , 'User-Agent': 'request'` but still getting 404 error - message: "Not Found" – ravindra devagiri Jun 04 '19 at 10:01
  • @ravindradevagiri, as advised in other comments, please check that you have the correct url or that the repo is enabled and available. – joran Jun 04 '19 at 11:36