2

I am creating a DHL API for checking shipment rates. I have uploaded my Node.js Server using putty and it is running correctly when I enter my IP address and port number.

In development, there are no erros or issues and I get back the response and get the rates back from the form I have set up for my users.

but in Production, when I upload my website to Hostinger I get this error " Failed to load resource: net::ERR_SSL_PROTOCOL_ERROR"

In production, it sends my post request with HTTPS instead of HTTP. And even when I change the HTTPS to HTTP in the browser I get another error saying "Cannot GET /api/dhl"

Here are some images to help make it more clear:

The Errors.

enter image description here

When my website submits the post request with HTTPS instead of HTTP.

enter image description here

When I manually change the URL from HTTPS to HTTP

enter image description here

What am I doing wrong? Why is this happening and how can I fix it?

Here is my code:

const express = require('express');
const port = 3001
app.post('/api/dhl', (req, res) => {

const accountNum = req.body.accountNum

const fromCountriesCode = req.body.fromCountriesCode
const fromCountriesCapital = req.body.fromCountriesCapital
const fromCity = req.body.fromCity
const fromPostalCode = req.body.fromPostalCode

const toCountriesCode = req.body.toCountriesCode
const toCountriesCapital = req.body.toCountriesCapital
const toCity = req.body.toCity
const toPostalCode = req.body.toPostalCode

const weight = parseInt(req.body.weight)
const plannedShippingDate = req.body.date

const len = "5"
const width = "5"
const height = "5"
const isCustomsDeclarable = 'false' 
const unitOfMeasurement = 'metric'

console.log(weight)
console.log(fromCountriesCode)
console.log(toCountriesCode)

console.log(fromCity)
console.log(toCity)

var options = { method: 'POST',
url: 'https://express.api.dhl.com/mydhlapi/rates',
headers: 
 { 'postman-token': '',
   'cache-control': 'no-cache',
   authorization: 'Basic myauthkey',
   'content-type': 'application/json' },
body: 
 { customerDetails: 
    { shipperDetails: 
       { postalCode: fromPostalCode,
         cityName: fromCity,
         countryCode: fromCountriesCode,
         addressLine1: '0' },
      receiverDetails: 
       { postalCode: toPostalCode,
         cityName: toCity,
         addressLine1: '0',
         countryCode: toCountriesCode }
         },
   accounts: [ { typeCode: 'shipper', number: 'my account number' } ],
   plannedShippingDateAndTime: '2021-08-25T13:00:00GMT+00:00',//Might need to change later
   unitOfMeasurement: 'metric',
   isCustomsDeclarable: true,
   monetaryAmount: [ { typeCode: 'declaredValue', value: 10, currency: 'BHD' } ],
   requestAllValueAddedServices: false,
   returnStandardProductsOnly: false,
   nextBusinessDay: false,
   packages: [ { weight: weight, dimensions: { length: 5, width: 5, height: 5 } } ] },
json: true };



request(options, function (error, response, body) {
    if (error) throw new Error(error);
    res.send(body)
    console.log(body);
  });
});


//Start the Server
app.listen(port, () => {
  console.log(`Server running at :${port}/`);
});

My Check-Rates File: const getRateEstimate = () => {

axios.post('http://MY_IP:3001/api/dhl', {

    fromCity,
    fromCountriesCapital,
    fromCountriesCode,
    fromPostalCode,

    toCountriesCapital,
    toCountriesCode,
    toPostalCode,
    toCity,

    weight,

 }).then(response => {

        console.log(response)
        setData(response.data);

 }).catch(e => {

     console.log(e)
 });

}

motionless570
  • 925
  • 1
  • 10
  • 26
  • Your server is not configured for HTTPS so of course HTTPS requests will fail. As for failing with plain HTTP - *"Cannot GET ...."* is too few information to debug the problem. – Steffen Ullrich Jan 23 '22 at 07:04
  • @SteffenUllrich yes I know my server is not HTTPS, the question is how do I make it HTTPS? – motionless570 Jan 23 '22 at 07:35
  • 1
    *"The question is how do I make it HTTPS?"* - A simple search for [expressjs https](https://www.google.com/search?q=expressjs+https) provides lots of useful hits, like the first one [Enabling HTTPS on express.js](https://stackoverflow.com/questions/11744975/enabling-https-on-express-js). I don't know if you missed these widely available information or if you have trouble following these. In this case please be more specific where you have trouble since it makes no sense to repeat what you already did not understand from other posts. – Steffen Ullrich Jan 23 '22 at 10:59

0 Answers0