0

The Library of Congress techcenter page at http://id.loc.gov/techcenter/ gives examples accessing linked data.

For example:

curl -L -H 'Accept: application/json' http://id.loc.gov/vocabulary/preservationEvents/creation

Running the above example returns a json response - I have done this.

But when I try a Nodejs script to access the same url - with the Accept header, it returns a "404 Not Found" error.

Here is my script:

'use strict';
const request = require('request');

var url = "http://id.loc.gov/vocabulary/preservationEvents/creation";
    var options = {
        url: url,
      headers: {
        "Accept": "application/json"
      }
    };
    request(
        options,
        (error, response, body) => {
            if (error) {
                return console.error(error);
            }
            if (response.statusCode == 200) {
                var resp = JSON.parse(body);
                console.log(resp);
                return console.log(body.substr(0, 128) + '...');
            }
            else {
                return console.error('Error: Response statusCode='+response.statusCode);
            }
        }
    );
    

I have tried this on my mac and also on a Digital Ocean Ubuntu server - both giving a 404 Not Found result.

If you have a minute, I would appreciate knowing if you were able to get a response using this script - or let me know if you see a bug.

Any help is appreciated.

Thanks

Colin Goldberg

Colin G
  • 309
  • 3
  • 14

1 Answers1

0

Looks like you need to set a User-Agent in your options sent in to request. For example, it works if you set it to the same as curl uses

var options = {
    url: url,
    headers: {
        "Accept": "application/json",
        "User-Agent": "curl/7.54.0" 
    }
};

Seems like you can pretty much set User-Agent to anything and it will work, as long as you set it.

axanpi
  • 731
  • 9
  • 16
  • Thank you so much - it works (with User-Agent "nodejs")! Your quick, correct response is very much appreciated. – Colin G Oct 16 '18 at 19:21