Issues Executing Query with API

818 views
Skip to first unread message

Allen Howard

unread,
Feb 7, 2017, 10:21:36 PM2/7/17
to Google Safe Browsing API
Hello,

So I recently started working with the Safe Browsing API in HTML/JQuery.  Right now, I'm just trying to get some results back with this code:

            function checkLink(){
                 
//Google API Key
               
var apiKey =  ""; //My actual key is in here
               
               
//URL for safebrowsing
               
var googleURL = "https://safebrowsing.googleapis.com/v4/threatMatches:find?key=";
               
               
//Combine the url and the key
                googleURL
= googleURL + apiKey;
               
               
//print this to the console
                console
.log(googleURL);
               
               
//attempt to contact the google server
               
//                var headers = {
//                   'content-type': 'applicataion/json'
//                };
               
               
var payload =
               
{
                   
"client": {
                     
"clientId": "infsci1073amh217",
                     
"clientVersion": "1.5.2"
                   
},
                   
"threatInfo": {
                     
"threatTypes":      ["MALWARE", "SOCIAL_ENGINEERING"],
                     
"platformTypes":    ["WINDOWS"],
                     
"threatEntryTypes": ["URL"],
                     
"threatEntries": [
                       
{"url": "http://www.pitt.edu/"},
                       
{"url": "http://www.exchange.pitt.edu/"}
                     
]
                   
}
                 
};
               
                $
.ajax({
                    type
: "POST",
                    url
: googleURL,
                    contentType
: "applicaiton/json; charset=utf-8",
                    dataType
: "json",
                    data
: payload,
                success
:function (data) {
                     console
.log(data);
               
},
                error
:function(status){
                    console
.log(status);
               
}
               
});
               
           
}

I get a 400 error saying "Invalid JSON payload received Unexpected token. \nclient%5BclientID%5D\n".

It sounds like when I sent this to the API that the API is rejecting it.  I can see that the key is being used in the API Manager.

Does anyone have any suggestions?  I tried running the API explorer using the provided code from the API Documentation and it is just spinning on the "executing" phase.

Thank you,

~Allen

Allen Howard

unread,
Feb 8, 2017, 10:49:10 AM2/8/17
to Google Safe Browsing API
In my code, I found one issue where I put applicaiton/json instead of application/json, but this did not fix the issue.  Still trying to get any ideas on this.

Thanks!

Alex Wozniak

unread,
Feb 8, 2017, 11:30:20 AM2/8/17
to Google Safe Browsing API
Hi Allen,

The error message you shared suggests that jQuery is doing some unexpected formatting of the request body. You could instead try passing JSON.stringify(payload) to the ajax's data parameter? If that doesn't work, you can use the Chrome Developer Tools to inspect the body of the produced request to make sure it looks well-formed.

Hopefully that helps!

Alex

On Wed, Feb 8, 2017 at 7:49 AM Allen Howard <allenh...@gmail.com> wrote:
In my code, I found one issue where I put applicaiton/json instead of application/json, but this did not fix the issue.  Still trying to get any ideas on this.

Thanks!

--
You received this message because you are subscribed to the Google Groups "Google Safe Browsing API" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-safe-browsi...@googlegroups.com.
To post to this group, send email to google-safe-...@googlegroups.com.
Visit this group at https://groups.google.com/group/google-safe-browsing-api.
For more options, visit https://groups.google.com/d/optout.

Allen Howard

unread,
Feb 8, 2017, 12:38:11 PM2/8/17
to Google Safe Browsing API
Thanks so much, Alex!  That worked!

I didn't realize that you could pass the stringed object through, I thought it needed the non-string version.

Best,
~Allen


On Wednesday, February 8, 2017 at 11:30:20 AM UTC-5, Alex Wozniak wrote:
Hi Allen,

The error message you shared suggests that jQuery is doing some unexpected formatting of the request body. You could instead try passing JSON.stringify(payload) to the ajax's data parameter? If that doesn't work, you can use the Chrome Developer Tools to inspect the body of the produced request to make sure it looks well-formed.

Hopefully that helps!

Alex

On Wed, Feb 8, 2017 at 7:49 AM Allen Howard <allenh...@gmail.com> wrote:
In my code, I found one issue where I put applicaiton/json instead of application/json, but this did not fix the issue.  Still trying to get any ideas on this.

Thanks!

--
You received this message because you are subscribed to the Google Groups "Google Safe Browsing API" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-safe-browsing-api+unsub...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages