4

I am trying to shorten a URL using goo.gl URL Shortener API an open source library (qwest.js). I've achieved it successfully using jquery though, but its giving me error "This API does not support parsing form-encoded input." when done using qwest.

My code with jquery :

var longURL = "http://www.google.com/"; $.ajax({ url: 'https://www.googleapis.com/urlshortener/v1/url?key=AIzaSyANFw1rVq_vnIzT4vVOwIw3fF1qHXV7Mjw', type: 'POST', contentType: 'application/json; charset=utf-8', data: '{ longUrl:"'+ longURL+'"}', success: function(response) { console.log(response) } }) .done(function(res) { console.log("success"); }) .fail(function() { console.log("error"); }) .always(function() { console.log("complete"); }); 

and the non-working code with qwest.js

var longURL = "http://www.google.com/" qwest.post('https://www.googleapis.com/urlshortener/v1/url?key=479dfb502221d2b4c4a0433c600e16ba5dc0df4e&', {longUrl: longURL}, {responseType:'application/json; charset=utf-8'}) .then(function(response) { // Make some useful actions }) .catch(function(e, url) { // Process the error }); 

any help would be highly recommended.

1 Answer 1

6

Author of qwest here ;)

As stated in the docs : the default Content-Type header is application/x-www-form-urlencoded for post and xhr2 data types, with a POST request.

But Google Shortener service does not accept it. I assume it wants a JSON input type. Then you should set the dataType option of qwest to json. Furthermore, your responseType option is invalid and does not follow the docs. Normally, you don't have to set it if Google replies to request with a valid Content-Type header. Here's the good code :

qwest.post('https://www.googleapis.com/urlshortener/v1/url?key=479dfb502221d2b4c4a0433c600e16ba5dc0df4e&', {longUrl: longURL}, {dataType:'json'})

In the case where Google does not send a recognized Content-Type, just set the responseType option to json too.

Sign up to request clarification or add additional context in comments.

4 Comments

hi there! thanks for{ dataType:'json'} . it worked. i also noticed i was using a wrong key too (different thing) good plugin
Thanks! Glad you've fixed your problem
Can we make synchronous calls using qwest like we do using ajax?
Yes, we can, by setting the async option to false.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.