Monday, October 16, 2017

Unable to trigger web request implemented in Giraffe

Leave a Comment

I'm struggling to trigger a web service that's written in Giraffe.

Here's the code:

let webApp : HttpContext -> HttpHandlerResult =       choose [         GET >=>             choose [ routef "/platforms/" fetchPlatforms ] 

fetchPlatforms is implemented as follows:

let private fetchPlatforms () (context : HttpContext) =     async { let response = getPlatforms()             return! json response context     } 

The issue that I'm running into is that when I run the server code and then attempt to test the web service, I get the following message:

enter image description here

Here's the entire solution.

Update:

I am only observing this issue in VS Code.

Hence, I can observe a successful web request and response using Visual Studio 2017 (3) version 15.4

Here's the video that demonstrates the difference between VS2017 and VS Code

2 Answers

Answers 1

I downloaded your source code and tried to reproduce the issue, but first I was getting an error during startup because I don't have your db configured locally, therefore I made the following change:

type TestObj =     {         Prop1 : string         Prop2 : int     }  let private fetchPlatforms  =     let obj1 = { Prop1 = "test"; Prop2 = 100 }     json obj1     //  let response = getPlatforms()     //  json response 

... and when I start the app now and make a Postman call to the /platforms endpoint then it seems to work fine for me...

postman

Have you resolved your issue perhaps?

Answers 2

routef should match pattern: https://github.com/dustinmoris/Giraffe#routef

otherwise try route:

let webApp : HttpContext -> HttpHandlerResult =   choose [     GET >=>         choose [ route "/platforms" >=> fetchPlatforms ] 

EDIT:

And looking at your code, the fetchPlatforms function is not a proper handler. HttpHandler :

A HttpHandler is a simple function which takes two curried arguments, a HttpFunc and a HttpContext, and returns a HttpContext (wrapped in an option and Task workflow) when finished.

Instead of:

let private fetchPlatforms  =      let response = getPlatforms()      json response 

Try something like that:

let fetchPlatforms   =     fun (next : HttpFunc) (ctx : HttpContext) ->         let response = getPlatforms()         json response next ctx 

You can do that async as well (it depends on getPlatforms):

let fetchPlatforms   =     fun (next : HttpFunc) (ctx : HttpContext) ->         task {             let! response = getPlatforms()             return! json response next ctx         } 
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment