0

I need to make a HTTP GET request but i have some trouble with making a querystring of a javascript object. This is my object.

var params = {
  from: {
      zip: '42100',
      country: 'IT'
  },
  to: {
      zip: '20019',
      country: 'IT'
  },
  packages: [ { "width": 50, "height": 40, "length": 40, "weight": 2 } ]
};

I manually made my querystring and this is the result

from[zip]=42100&from[country]=IT&to[zip]=20019&to[country]=IT&packages[0]=[width]=50&[height]=40&[length]=40&[weight]=2

The problem is that Google says "Your client has issued a malformed or illegal request."

This is my NodeJS script.

var request = require('request');

var params = {
    from: {
        zip: '42100',
        country: 'IT'
    },
    to: {
        zip: '20019',
        country: 'IT'
    },
    packages: [ { "width": 50, "height": 40, "length": 40, "weight": 2 } ]
};

function packagesToQueryString(packages) {
    let stringa = "";
    for (const onePackage of packages) {
        stringa += '[width]='+ onePackage.width + '&[height]='+ onePackage.height +'&[length]='+ onePackage.length +'&[weight]='+ onePackage.weight +'';
    }
    return 'packages[' + (packages.length - 1) + ']=' + stringa;
}


function paramsToQueryString(obj) {
    return 'from[zip]=' + obj.from.zip +'&from[country]=' + obj.from.country + '&to[zip]=' + obj.to.zip + '&to[country]=' + obj.to.country+ '&';
}

const formData = paramsToQueryString(params) + packagesToQueryString(params.packages);
console.log(formData);

request({
    headers: {
        'Authorization': 'fcd3dda8...2577',
        'Content-Type': 'application/json'
    },
    body: formData,
    uri: 'https://api.packlink.com/v1/services',
    method: 'GET'
}, function (err, res, body) {
    console.log(body);
});

Google error

3
  • What do you mean by "Google says..."? Is it a warning from Chrome? Can you provide some screenshot? Commented Apr 30, 2019 at 1:46
  • google error added Commented Apr 30, 2019 at 11:02
  • I can see what's going wrong now. Please check my answer. Commented Apr 30, 2019 at 14:02

1 Answer 1

1

There are 2 reasons that causes the "malformed or illegal request" error:

  1. In your HTTP request, the Content-Type is defined as application/json. However, the value of body is formData (from[zip]=42100&from[country]=...), which is NOT json.
  2. Even for the formData, your assemble logic is incorrect. For params object, its corresponding query string is: from%5Bzip%5D=42100&from%5Bcountry%5D=IT&to%5Bzip%5D=20019&to%5Bcountry%5D=IT&packages%5B0%5D%5Bwidth%5D=50&packages%5B0%5D%5Bheight%5D=40&packages%5B0%5D%5Blength%5D=40&packages%5B0%5D%5Bweight%5D=2

In order to successfully send that HTTP GET request, you need to use the qs option of request module. The code would look like below:

var request = require('request');

var params = {
  from: {
    zip: '42100',
    country: 'IT'
  },
  to: {
    zip: '20019',
    country: 'IT'
  },
  packages: [ { "width": 50, "height": 40, "length": 40, "weight": 2 } ]
};

request({
  headers: {
    'Authorization': 'fcd3dda8...2577'
  },
  qs: params,
  uri: 'https://api.packlink.com/v1/services',
  method: 'GET'
}, function (err, res, body) {
  console.log(body);
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.