4

I have written the following javascript to create a tasklist in google:

postData = {'title':'Netsuite List'};
access_token = 'xxxx';

url = 'https://www.googleapis.com/tasks/v1/users/@me/lists';

headers['Content-type'] = 'application/json';
headers['Authorization'] = 'Bearer ' + access_token;
headers['Content-length'] = 25; 
response = $$.requestURL(url, postData, headers, 'POST');

The response says:

{ "error": 
{ "errors": [ { "domain": "global", "reason": "parseError", "message": "This API does not support parsing form-encoded input." } ], "code": 400, "message": "This API does not support parsing form-encoded input." } 
}

What could be the possible error ?

Anshul
  • 109
  • 1
  • 11

3 Answers3

2

not working

contentType: 'application/json; charset=UTF-8',

try with this

var headers = { };

headers["Content-Type"] ="application/json ; charset=UTF-8"; 
//remove to parsing form-encoded input error

data:JSON.stringify( model),
//this use for remove to parse error

Example:

$.ajax({
    type: 'Post',
    url: postUrl,
    headers: headers,
    dataType: 'json',//not required in some case
    data:JSON.stringify( model),                
    success: function (data, sts) {
        alert('success');
    },
    error: function (err, sts) {
        var msg;
    }
});
jwpfox
  • 5,124
  • 11
  • 45
  • 42
Navdeep Kapil
  • 341
  • 3
  • 5
0

You sent data like:

title=Netsuite%20List

But Google API waits for JSON:

{ "title": "Netsuite List" }

Try to provide JSON.stringify() output to the requestURL method:

postData = JSON.stringify({'title':'Netsuite List'});          // <-- Added JSON.stringify
access_token = 'xxxx';

url = 'https://www.googleapis.com/tasks/v1/users/@me/lists';

headers['Content-type'] = 'application/json';
headers['Authorization'] = 'Bearer ' + access_token;
headers['Content-length'] = 25; 
response = $$.requestURL(url, postData, headers, 'POST');

Also, it's better to get around documentation or source of $$ object you use and check how it can support sending JSON data.

terales
  • 3,116
  • 23
  • 33
0
jQuery.ajax({
    url: "https://www.googleapis.com/tasks/v1/users/@me/lists",
    method: "POST",
    data: JSON.stringify({ /* your object */ }),
    dataType: "json",
    beforeSend: (xhr) => {
        xhr.setRequestHeader("Content-Type", "application/json");
    },
    //...

or :

jQuery.ajax({
    url: "https://www.googleapis.com/tasks/v1/users/@me/lists",
    method: "POST",
    data: JSON.stringify({ /* your object */ }),
    dataType: "json",
    contentType: "application/json",
    //...
user2226755
  • 12,494
  • 5
  • 50
  • 73