Reputation: 765
I'm having trouble consuming the google safe browsing api v4. I'm sure this should be easy but its already taking me some time. I always get error 400 bad request. here is my code :
var
idHttp : TIdHTTPEx;
url : string;
slTemp : TStringList;
memoryStream : TMemoryStream;
begin
idHttp := TIdHTTPEx.Create(nil); // parent class is TIdHTTP, inherited to be able to execute HTTPS
slTemp := TStringList.Create;
memoryStream := TStringStream.Create;
try
idHttp.Request.ContentType := 'application/json';
url := 'https://safebrowsing.googleapis.com/v4/threatMatches:find?key=' + GOOGLE_SAFE_BROWSING_API_KEY + ' HTTP/1.1';
slTemp.Text := '{"client":{"clientId":"iGame","clientVersion":"1.0.0"},"threatInfo":{"threatTypes":"MALWARE","platformTypes":"WINDOWS","threatEntryTypes":"URL","threatEntries":[{"url":"http://www.hyxyg.com/default.php"},{"url":"https://jsonlint.com/"}]}}';
idHttp.Post(url, slTemp, memoryStream);
memo1.Text := memoryStream.ToString;
finally
memoryStream.Free;
slTemp.Free;
idHttp.Free;
end;
end;
I tried checking this but it is using lower version. Where did I go wrong?
EDIT
I tried using this as suggested by one of the comment, but still the same error 400 bad request. the example was OK though.
var
idHttp : TIdHTTPEx; // parent class is TIdHTTP, inherited to be able to execute HTTPS
url : string;
requestBody : TStream;
sTemp : string;
begin
url := 'https://safebrowsing.googleapis.com/v4/threatMatches:find?key=' + GOOGLE_SAFE_BROWSING_API_KEY + ' HTTP/1.1';
//url := 'https://httpbin.org/post';
idHttp := TIdHTTPEx.Create(nil);
requestBody := nil;
try
idHttp.Request.Accept := 'application/json';
idHttp.Request.ContentType := 'application/json';
sTemp := '{"client":{"clientId":"iGame","clientVersion":"1.0.0"},"threatInfo":{"threatTypes":"MALWARE","platformTypes":"WINDOWS","threatEntryTypes":"URL","threatEntries":[{"url":"http://www.hyxyg.com/default.php"},{"url":"https://jsonlint.com/"}]}}';
//sTemp := '{"日本語":42}';
requestBody := TStringStream.Create(sTemp, TEncoding.UTF8);
sTemp := idHttp.Post(url, requestBody);
memo1.Text := sTemp + sLineBreak + sLineBreak + idHttp.ResponseText;
finally
requestBody.Free;
idHttp.Free;
end;
end;
Upvotes: 1
Views: 160
Reputation: 840
The " HTTP/1.1" at the end of the URI must be an error, try without it.
Upvotes: 1