Suave.Json.mapJson maps the input JSON to an object into your function, then maps the output of your function into JSON.
The problem is that I'm happy with the way it maps into my function, but I need to return a json string response rather than have suave serialise my output into JSON for me. How can I do this?
Currently i'm getting my output serialised twice. My code so far:
let executeQuery : Query -> string = //Query is my deserialised json input, the return value is a json string
let app = POST >=> path "/graphql" >=> Json.mapJson executeQuery >=> setMimeType "application/json; charset=utf-8"
startWebServer defaultConfig app
If you look at the Suave source code, you'll see that mapJson is shorthand for mapJsonWith fromJson toJson. The fromJson and toJson functions are the default JSON deserializer and serializer (respectively), but you could create your own instead -- or just use id to say "map this direction without changing it". E.g.,
let oneWayMapJson = mapJsonWith fromJson id
Note that I haven't tested this, just typed it into the Stack Overflow answer box, so some tweaking may be required. I don't have time to expand on this answer right now, but if you need more help than this rather barebones answer, let me know and I'll try to give you more help sometime tomorrow.
Related
I have a test where after getting a response I would like to validate the entire schema of the response (not individual response node/value comparison).
Sample test:
[<Test>]
let howtoValidateSchema () =
let request = Request.createUrl Post "https://reqres.in/api/users"
|> Request.setHeader (Accept "application/json")
|> Request.bodyString """{"name": "morpheus",
"job": "leader"}"""
|> Request.responseAsString
|> run
Is there a way that I can save my expected Schema somewhere and once I get the response I do the comparison to check that response has same number of nodes (neither less nor more than expected schema)?
I am ok to opt for other libs like FSharp.Data if we there is no direct way in HttpFs.Client. I looked at FSharp.Data (https://fsharp.github.io/FSharp.Data/library/JsonProvider.html) but not able to seek how it meets the requirements where the schema comparison needs to be done with the savedExpectedSchemaJson=ResponseJson.
You can use Newtonsoft.Json.Schemato validate schemas:
open Newtonsoft.Json.Schema
open Newtonsoft.Json.Linq
let schema = JSchema.Parse expectedSchema
let json = JObject.Parse responeJson
let valid = json.IsValid schema
However this assumes you have a schema predefined somewhere. If you don't have such schema is best to use the JsonProvider who can infer it for you.
Run the call manually and save the result in a sample.json file and create a type using the JsonProvider:
type ResponseSchema = JsonProvider<"sample.json">
and you can use this type to parse any new content based on the sample (provided that the sample is a representative.
ResponseSchema.parse response
This won't validate the schema but will try to meet as best as it can given the input.
I have a Map object
Map decodedresp = json.decode(response);
from here i get a list :
print(decodedresp['objectUrls']);
[{signedUrl: abc.com, path: a/b/c.log}]
now I want to get abc.com in a string, and I am not able to get it
I am learning dart and I am new to it
So, from what I can see it looks like you're dealing with parsed json.
In decoding json like you did json.decode(response) you're getting a:
_InternalLinkedHashMap<String, dynamic>
From that you are accessing objectUrls which returns a List<Map>. In order to deal with that you need to look for the key which in this case is signedUrl or path. Because your Map is wrapped in a List you need to get the element of the List.
This should work for you
Map<String, dynamic> decodresp = json.decode(response);
List<dynamic> objectUrls = decoderesp['objectUrls'];
// Zero here to get the first element, followed by the key.
var signedUrl = objectUrls[0]["signedUrl"];
var path = objectUrls[0]["path"];
You can see it working here
I could be wrong but because there isn't much info this is what I'm going with.
Let me know if you have any problems.
Felix.
def siteNameChange():File={
for(line<-Source.fromFile("RecordedSimulation_0000_NewSiterequest2.txt").getLines())
if(line.contains("siteUrl"))
println(line)
return new File("RecordedSimulation_0000_NewSiterequest2.txt")
}
val scn = scenario("RecordedSimulation")
.exec(http("request_0")
.post(“/student/new”)
.body(RawFileBodyPart(session=>siteNameChange())).asJSON)
Hello I am a newbie to Gatling, using it for performance testing. I have a function named siteNameChange() which returns a file after doing some modifications on the file.
This function I am calling in the scenario body to send the data.
But when I am running the script I am getting scala:48:26: missing parameter type
.body(RawFileBodyPart(session=>siteNameChange())).asJSON)
Can some one please suggest whats the best thing to do this here, how to get the function return the modified file and pass the file data over the post request
body doesn't take a BodyPart (which is for multipart) parameter but a Body one.
You should be passing a RawFileBody.
I have the following F# code attempting to get a user document from my documentdb database and it doesn't work. Is anyone else experiencing problems using the ReadDocumentAsync method? I am able to successfully to query my user documents with the CreateDocumentQuery method. Any help is greatly appreciated. I use my database and collection ids in place of the empty strings in the code snippet
let getUserDatabaseModel (documentClient : DocumentClient) originiatorId =
async {
let databaseId = ""
let collectionId = ""
let documentUri = UriFactory.CreateDocumentUri(databaseId, collectionId, originiatorId)
let! userDatabaseModel =
documentClient.ReadDocumentAsync(documentUri)
|> Async.AwaitTask
return userDatabaseModel
}
|> Async.RunSynchronously
Update
If I use the _rid instead of the id I get the data back. To clarify the ReadDocumentAsync seems to work using the _rid but throws the error below when using the id.
mscorlib: Exception has been thrown by the target of an invocation.
FSharp.Core: One or more errors occurred. Microsoft.Azure.Documents.Client:
The value 'left blank intentionally' specified for
query '$resolveFor' is invalid
I posted a while back on the documentdb github issues page, problems I was having with ReplaceDocumentAsync.
https://github.com/Azure/azure-documentdb-dotnet/issues/113
I wasn't too concerned about the fix because there was UpsertDocumentAsync. This issue seems to be related, or maybe their relation is just me! not being able to figure out what I'm doing wrong.
CreateDocumentUri needs consistent ids, either all ids or all _rids for the database, collection, and document
I would like to use non-literal strings for the "format" parameter of a logging type function, as shown here:
// You need to make c:\testDir or something similar to run this.....
//
let csvFile = #"c:\testDir\foo.csv"
open System.IO
let writer file (s:string) =
use streamWriter = new StreamWriter(file, true)
streamWriter.WriteLine(s)
// s
let log format = Printf.ksprintf (writer csvFile) format
let oneString format = (Printf.StringFormat<string->string> format)
let format = oneString "(this does not %s)"
//log format "important string"
log "this works %s" "important string"
My first attempt used a literal string, and the above fragment should work fine for you if you create the directory it needs or similar.
After discovering that you can't just "let bind" a format string, I then learned about Printf.StringFormatand more details about Printf.ksprintf, but I am obviously missing something, because I can't get them to work together with my small example.
If you comment out the last line and reinstate its predecessor, you will see a compiler error.
Making the function writer return a string almost helped (uncomment its last line), but that then makes log return a string (which means every call now needs an ignore).
I would like to know how to have my format strings dynamically settable within the type checked F# printf world!
Update
I added the parameter format to log to avoid a value restriction error that happens if log is not later called as it is in my example. I also change fmt to format in oneString.
Update
This is a different question from this one. That question does not show a function argument being passed to Printf.StringFormat (a minor difference), and it does not have the part about Printf.ksprintf not taking a continuation function that returns unit.
I thought I had found a solution with:
let oneString format = (Printf.StringFormat<string->string,unit> format)
this compiles, but there is a runtime error. (The change is the ,unit)