Problems with the request npm module

Asked

Viewed 61 times

0

I’m having some difficulty making HTTP requests.

The code:

import Request from 'Request';

function xxx (tomboCodBarra){
    const options = {
        url: `http://servicos.jbrj.gov.br/v2/herbarium/${tomboCodBarra}`,
        method: 'GET',
        headers: {
            Accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
            'Content-type': 'application/json',
            'Accept-Encoding': 'gzip, deflate',
            'Accept-Language': 'pt-BR,pt;q=0.8,en-US;q=0.5,en;q=0.3',
            'Accept-Charset': 'utf-8',
            Connection: 'keep-alive',
            Host: 'servicos.jbrj.gov.br',
            'Upgrade-Insecure-Requests': '1',
        },
    };
    request(options, (err, res, body) => {
        const json = JSON.parse(body);
        console.log(json);
    });
}

So, an example of tomboCodBarra is RB01364063. The output of this code should be a JSON, as follows below:

{
  "success": true,
  "result": [
    {
      "occurrenceid": 4799256,
      "rightsholder": "RB",
      "bibliographiccitation": "Herbário Virtual REFLORA Disponível em: http://herbariovirtualreflora.jbrj.gov.br/reflora/herbarioVirtual/ConsultaPublicoHVUC/ConsultaPublicoHVUC.do?idTestemunho=4799256",
      "modified": null,
      "institutioncode": "RB",
      "collectioncode": "RB",
      "catalognumber": "RB01364063",
      "occurrenceremarks": "",
      "recordedby": "P.E. Ellert- Pereira;G. Heiden,L.H. Dal Molin,M.P. Estabão",
      "recordnumber": "86",
      "othercatalognumbers": "",
      "eventdate": "10/8/2015",
      "year": 2015,
      "month": 8,
      "day": 10,
      "verbatimeventdate": "10/08/2015",
      "fieldnotes": "Planta até 3 metros de altura. Frutos imaturos verdes; quase maduros roxo-esverdeados a quase completamente roxos",
      "country": "Brasil",
      "countrycode": "BR",
      "stateprovince": "Goiás",
      "municipality": "Jataí",
      "locality": "Saída em direção a Rio Verde. Rancho Alto das Rosas",
      "minimumelevationinmeters": null,
      "maximumelevationinmeters": null,
      "verbatimlatitude": null,
      "verbatimlongitude": null,
      "decimallatitude": "-17.9161111111111",
      "decimallongitude": "-51.6952777777778",
      "identifiedby": "P.E. Ellert- Pereira",
      "identificationverificationstatus": null,
      "dateidentified": "10/8/2015",
      "identificationremarks": "",
      "identificationqualifier": null,
      "typestatus": null,
      "scientificname": "Arecaceae Butia purpurascens Glassman",
      "family": "Arecaceae",
      "genus": "Butia",
      "specificepithet": "purpurascens",
      "infraspecificepithet": null,
      "taxonrank": "ESPECIE",
      "scientificnameauthorship": "Glassman",
      "type": null,
      "rights": "Creative Commons",
      "associatedmedia": "http://imagens1.jbrj.gov.br/fsi/server?type=image&source=rb/1/36/40/63/01364063-3.jpg|http://imagens1.jbrj.gov.br/fsi/server?type=image&source=rb/1/36/40/63/01364063-2.jpg|http://imagens1.jbrj.gov.br/fsi/server?type=image&source=rb/1/36/40/63/01364063-1.jpg|http://imagens1.jbrj.gov.br/fsi/server?type=image&source=rb/1/36/40/63/01364063-4.jpg|http://imagens1.jbrj.gov.br/fsi/server?type=image&source=rb/1/36/40/63/01364063.jpg",
      "eventremarks": null
    }
  ]
}

But sometimes it does not return the above JSON, but the below:

{ erro: '500',
  message: 'Oops, something\'s gone wrong in server!' }

So some ideas that I tried to do so that this JSON is not the returned was:

  • Use other modules such as got, axios, node-wget, superagent, but the same mistake happens;
  • I tried to combine these modules, if one of these returned the unexpected JSON I would go to the other modules, but it didn’t work;
  • I also used other modules, such as sync-request, sync-exec (the sync-exec I used to realize the curl or wget to be able to complete the requisition), but it made a mistake of time out;
  • I also used other modules, such as http, httpclient, http-client(something like this, I don’t remember their names), but they didn’t return error, neither JSON with error 500 nor the correct JSON;
  • I used the module python-shell, in which I made a program in Python that only made the request and gave a print in the answer, however JSON with error 500 was returned and also returned an error of time out;
  • At one place I saw that adding request parameters might solve this problem, so I made a request by Firefox and took the parameters that were used, but did not solve.
  • I tried to put in a infinite loop where it would only come out of this loop if it returned the expected JSON, but ended up returning errors related to time out;
  • I tried running on Windows 7 64 bits (currently running on Linux Mint 19 Tara 64 bits), but nothing.
  • AND THE MOST INTERESTING is that I made two programs in Java and Python (the part of my code in Javascript), which made the request of 20 thousand (which is how much I need) barcodes, but they did not return this JSON, with error code 500;

So my version of Node is V10.15.0. Does anyone have any idea of a module I can use or any idea how to make this request?

  • Xandon, welcome to Stack Overflow in English, I edited your question by removing noises ( greeting, etc ) and also irrelevant tags. Read the [tour] to learn how the site works and if you need help on how to make use of the tools, go to [help].

  • All right, thank you!

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.