2

Google is returning a 400 bad request; but what is wrong with the request?

open FSharp.Data

let apiKey = "key goes here - removed for stackoverflow"

let postUrl = "http://safebrowsing.clients.google.com/safebrowsing/downloads"
let testArray = "2\nhttp://www.google.com/\nhttp://ianfette.org/"

[<EntryPoint>]
let main argv = 

    let foo2 = Http.Request(postUrl, httpMethod = "Post",
        query   = [ "client", "api"; "apikey", apiKey; "appver", "1.0"; "pver", "2.2" ],
        body = TextRequest (testArray)
        )

    0

I have verified that my key is correct by successfully executing get requests, it is only the post that is failing.

Tomas Petricek
  • 240,744
  • 19
  • 378
  • 553
JonnyBoats
  • 5,177
  • 1
  • 36
  • 60
  • I think that a reason in the incorrect request body. If you change it like this ```let testArray = "goog-malware-shavar;\ngoogpub-phish-shavar;"``` (body from google example) then it will work. – Sergey Tihon Mar 29 '14 at 20:59
  • Actually "The request’s body contains several lines separated by LF. The first line is a number indicating how many URLs are included in the body." https://developers.google.com/safe-browsing/lookup_guide#AQuickExamplePOSTMethod – JonnyBoats Mar 29 '14 at 22:19
  • Yes, but for this call you should change `postUrl` and version numbers: ```let postUrl = "https://sb-ssl.google.com/safebrowsing/api/lookup"``` and ```... "appver", "1.5.2"; "pver", "3.0"...```. – Sergey Tihon Mar 29 '14 at 22:26
  • Sergey: Good catch, I did have the wrong URL. Unfortunately I still can't get this to work. – JonnyBoats Mar 30 '14 at 00:33

1 Answers1

2

When I updated FSharp.Data to version 2.0.5 (released 2014-03-29) this started working. I can only assume that there was a bug in the previous version that is now fixed.

release notes state:

Added - to the list of default missing values. Re-added support for specifying known HTTP headers in the wrong case. Fixed sending of HTTP requests when using a portable class library version of FSharp.Data in the full .NET version.

Here is the final (working) code:

open FSharp.Data

let apiKey = "key goes here"

let postUrl = "https://sb-ssl.google.com/safebrowsing/api/lookup"
let testArray = "2\nhttp://www.google.com/\nhttp://ianfette.org/"

[<EntryPoint>]
let main argv = 

    let foo2 = Http.Request(postUrl, httpMethod = "Post",
        query   = [ "client", "api"; "apikey", apiKey; "appver", "1.5.2"; "pver", "3.0" ],
        body = TextRequest (testArray)
        )

    0

Thank you to Sergey Tihon for finding the error in my URL string in the question.

JonnyBoats
  • 5,177
  • 1
  • 36
  • 60
  • Humm, I have no idea how my fix solved your problem, but I'm glad it did :) Were you using the net40 version or any of the pcl's? – Gustavo Guerra Mar 30 '14 at 14:40
  • Gustavo: Using .Net framework 4.5.1 and F# 3.1 (FSHarp.Core 4.3.1.0) Added FSharp.Data using NuGet Package Manager in Visusl Studio 2013 Ultimate, always using latest stable version. – JonnyBoats Mar 30 '14 at 18:28