Frage

I signed up for the yellow pages.com API program found here: https://publisher.yp.com/home.

I went to make a call like this and I am getting back JSON in the browser: http://pubapi.atti.com/search-api/search/devapi/search?term=Jumbo+China+5&searchloc=6108+Falls+Of+Neuse+Rd+27609&format=json&key=ZZZZZZZZ

When I take the json results and put it into Json2CSharp, it renders fine. When I try and load it into a type provider:

type RestaurantListingJson = JsonProvider< @"http://pubapi.atti.com/search-api/search/devapi/search?term=Jumbo+China+5&searchloc=6108+Falls+Of+Neuse+Rd+27609&format=json&key=ZZZZZZZ">

I get a 400

Looking at fiddler, I see "User agent is a required field"

Has anyone run into this before? How do I add a user agent to a type provider?

Thanks in advance

War es hilfreich?

Lösung 2

The latest version of F# Data now always sends user agent and accept headers, so this should now work directly:

type RestaurantListingJson = JsonProvider<"http://pubapi.atti.com/search-api/search/devapi/search?term=Jumbo+China+5&searchloc=6108+Falls+Of+Neuse+Rd+27609&format=json&key=ZZZZZZZ">

Andere Tipps

I have not created an account, so I could not try this - but if the error message says "user agent is a required field", then I guess that the service requires setting the User-Agent header of the HTTP request.

This is not supported in static parameters of the JsonProvider, so the best way to get this to work would be to download the sample JSON, save it to a local file (say yp.json) and then use that to create the type provider:

type Yp = JsonProvider<"yp.json">

To actually download some data (when you want to make a request), you can use Http.RequestString which takes headers - there you can specify any required headers including User-Agent:

let response = 
  Http.RequestString("http://httpbin.org/user-agent", headers=["user-agent", "test"])

Then you can load the data using Yp.Parse(response) (rather than using the Load method directly to request a URL which would not let you specify the header).

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top