-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestClient.fsx
More file actions
73 lines (61 loc) · 2.29 KB
/
Copy pathTestClient.fsx
File metadata and controls
73 lines (61 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
open System.Text
open System.Net
open System.Net.Http
open System.Net.Http.Headers
open System
open System.IO
open System.Text.Encodings.Web
open System.Text.Json
open System.Text.Unicode
// if SSL error: dotnet dev-certs https --trust
type Book = {
author: string
title: string
publisher: string
yearofpublication: string
ISBN: string
}
let PrintResponse (resp:HttpResponseMessage) =
async{
let! response_content = resp.Content.ReadAsByteArrayAsync() |> Async.AwaitTask
printfn "Status Code: %d" (int resp.StatusCode)
(*printfn "Headers: "
for elem in resp.Content.Headers do
printf " %s: " elem.Key
for el in elem.Value do
printf "%s, " el
printf "\n"
for elem in resp.Headers do
printf " %s: " elem.Key
for el in elem.Value do
printf "%s, " el
printf "\n"*)
printf "Content:\n %s" (Encoding.UTF8.GetString(response_content))
}
let DeleteByID (id:int) =
async{
let client = new HttpClient()
let! response = (sprintf "http://localhost:5000/bookshelf/delete/%d" id) |> client.DeleteAsync |> Async.AwaitTask
PrintResponse response |> Async.RunSynchronously
}
let PostNewBook book =
async{
let client = new HttpClient()
let! response = client.PostAsync("https://localhost:5001/bookshelf/addbody", new StringContent(book, Encoding.UTF8, "application/json")) |> Async.AwaitTask
PrintResponse response |> Async.RunSynchronously
}
// DELETE some book
let Delete nr =
DeleteByID nr |> Async.RunSynchronously
// Add new book
let Post ksiazka =
let options = new JsonSerializerOptions()
options.Encoder <- JavaScriptEncoder.Create(UnicodeRanges.BasicLatin, UnicodeRanges.Latin1Supplement, UnicodeRanges.LatinExtendedA)
//options.WriteIndented <- true // only if want to printf serialized msg
let msg = Encoding.UTF8.GetString(Json.JsonSerializer.SerializeToUtf8Bytes(ksiazka, options))
printfn "%s" msg
PostNewBook msg |> Async.RunSynchronously
// Odkomentuj poniższe linie aby wykonać odpowiednie funkcje
//Delete 5
let ksiazka = { author = "Autor"; title="Tytul"; publisher="Wydawca"; yearofpublication="2020"; ISBN=null}
Post ksiazka