From 9f5ec29cd6c632d4349c0bdd410887d6ffe9dbc8 Mon Sep 17 00:00:00 2001 From: Grant Smith <57376089+ggsmith842@users.noreply.github.com> Date: Thu, 20 Jun 2024 08:08:22 -0600 Subject: [PATCH 01/17] Update tasks.yml --- data/cookbook/tasks.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/data/cookbook/tasks.yml b/data/cookbook/tasks.yml index 5ef3052485..7f0a22ee25 100644 --- a/data/cookbook/tasks.yml +++ b/data/cookbook/tasks.yml @@ -230,6 +230,8 @@ categories: slug: download-file-to-temporary-dir - title: Make a Partial Download with HTTP Range Header slug: make-partial-download-with-http-range-header + - title: Make a HTTP POST Request + slug: make-http-post-request - title: Dealing with HTML tasks: - title: Render a HTML Template From a33c93a2facf85ece33d0b0458539a2158457cb1 Mon Sep 17 00:00:00 2001 From: Grant Smith <57376089+ggsmith842@users.noreply.github.com> Date: Thu, 20 Jun 2024 10:12:17 -0600 Subject: [PATCH 02/17] Update tasks.yml Add bearer token as authentication type --- data/cookbook/tasks.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/data/cookbook/tasks.yml b/data/cookbook/tasks.yml index 7f0a22ee25..9ef973a56a 100644 --- a/data/cookbook/tasks.yml +++ b/data/cookbook/tasks.yml @@ -230,8 +230,8 @@ categories: slug: download-file-to-temporary-dir - title: Make a Partial Download with HTTP Range Header slug: make-partial-download-with-http-range-header - - title: Make a HTTP POST Request - slug: make-http-post-request + - title: Make a HTTP POST Request with Bearer Token Authentication + slug: make-http-post-request-token-auth - title: Dealing with HTML tasks: - title: Render a HTML Template From bb050b5de522b2ce361d36deea72c43e896a61f0 Mon Sep 17 00:00:00 2001 From: ggsmith842 Date: Thu, 20 Jun 2024 21:56:15 -0600 Subject: [PATCH 03/17] Add 00-cohttp_lwt.ml --- data/cookbook/http-rest/00-cohttp_lwt.ml | 75 ++++++++++++++++++++++++ data/cookbook/tasks.yml | 4 +- 2 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 data/cookbook/http-rest/00-cohttp_lwt.ml diff --git a/data/cookbook/http-rest/00-cohttp_lwt.ml b/data/cookbook/http-rest/00-cohttp_lwt.ml new file mode 100644 index 0000000000..5a98a950b9 --- /dev/null +++ b/data/cookbook/http-rest/00-cohttp_lwt.ml @@ -0,0 +1,75 @@ +--- +packages: +- name: "lwt" + tested_version: "5.7.0" + used_libraries: + - lwt +- name: "cohttp" + tested_version: "5.3.1" + used_libraries: + - cohttp +- name: "cohttp-lwt-unix" + tested_version: "5.3.1" + used_libraries: + - cohttp-lwt-unix +- name: "tls" + tested_version: "0.17.5" +- name: "tls-lwt" + tested_version: "0.17.5" +- name: "ssl" + tested_version: "0.7.0" +discussion: | + - **REST API Operations - POST:** A `POST` operation creates a *new* resouce. Ex. Add data to a database. + - **SSL/TLS Note:** Running this example may result in an error: `Exception: Failure “No SSL or TLS support compiled into Conduit`. + To resolve this issue you can run `opam install ssl tls-lwt` + - **The code below uses the GitHub REST API as an example. Please review the documentation here: [github.com/restap/issues/comments](https://docs.github.com/en/rest/issues/comments?apiVersion=2022-11-28#create-an-issue-comment) +--- + +open Lwt +open Cohttp +open Cohttp_lwt_unix + +(* create the message payload for our POST request. This can vary from API to API so be +sure to review the API's documentation on what it expects to receive*) +let message_body = ref "{\"body\":\"This is a good issue to work on!\"}" + +(* `request_body` contains our API endpoint `uri`. This example uses the Github issue comments +api endpoint. You will need to fill in ``, ``, and `` with +your own information from Github.*) +let request_body = + let uri = + Uri.of_string + "https://api.github.com/repos///issues//comments" + in + (* define headers using `Header.init` and `Header.add`. Since we are using Bearer Token authentication, + we need to add the `Authorization` header which has the form `Bearer `. Please ensure you always + keep your token secret. You can refer to the `read-environment-variable` section in the cookbook for one way to protect + your token*) + let headers = + Header.init () + |> fun h -> + Header.add h "Accept" "application/vnd.github+json" + |> fun h -> + Header.add h "Authorization" "Bearer " + |> fun h -> Header.add h "X-GitHub-Api-Version" "2022-11-28" + in + (* initialize the `body` to contain the json formatted text defined earlier in `message_body`. We tell the client + we are making a `POST` call and pass the headers and body. `Client.call` performs the operation + and returns a `response` and a `code` to let us know if our call worked as expected (i.e. 201 means the call created a resource). *) + let body = Cohttp_lwt.Body.of_string !message_body in + Client.call ~headers ~body `POST uri + >>= fun (response, body) -> + let code = response |> Response.status |> Code.code_of_status in + body |> Cohttp_lwt.Body.to_string >|= fun body -> code, body +;; + +(* `Lwt_main.run` executes our call defined in the `request_body` and then + we print the `response_code` and the `response_body` *) +let () = + let response_code, response_body = Lwt_main.run request_body in + Printf.printf "Respose code: %d\n" response_code; + Printf.printf "Response body: %s\n" response_body +;; + + + diff --git a/data/cookbook/tasks.yml b/data/cookbook/tasks.yml index 9ef973a56a..d981e07a8b 100644 --- a/data/cookbook/tasks.yml +++ b/data/cookbook/tasks.yml @@ -231,7 +231,9 @@ categories: - title: Make a Partial Download with HTTP Range Header slug: make-partial-download-with-http-range-header - title: Make a HTTP POST Request with Bearer Token Authentication - slug: make-http-post-request-token-auth + slug: make-http-post-request-bearer-auth + description: > + Make an HTTP POST request then print response code and body - title: Dealing with HTML tasks: - title: Render a HTML Template From a015fdba4914a7a3ece98d246c7fecc5e4d5db8c Mon Sep 17 00:00:00 2001 From: Christine Rose Date: Fri, 21 Jun 2024 01:13:30 -0700 Subject: [PATCH 04/17] a to an for HTTP --- data/cookbook/tasks.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data/cookbook/tasks.yml b/data/cookbook/tasks.yml index d981e07a8b..75bb6a5dc7 100644 --- a/data/cookbook/tasks.yml +++ b/data/cookbook/tasks.yml @@ -224,7 +224,7 @@ categories: slug: make-http-get-request description: > Make an HTTP GET request, process response code, follow redirects - - title: Make a HTTP GET Request with Basic Authentication + - title: Make an HTTP GET Request with Basic Authentication slug: make-http-get-basic-auth - title: Download a File to a Temporary Directory slug: download-file-to-temporary-dir From 61a290171ad0dbef7de130c021091988e7aafca7 Mon Sep 17 00:00:00 2001 From: Christine Rose Date: Fri, 21 Jun 2024 01:16:57 -0700 Subject: [PATCH 05/17] minor formatting, etc. --- data/cookbook/http-rest/00-cohttp_lwt.ml | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/data/cookbook/http-rest/00-cohttp_lwt.ml b/data/cookbook/http-rest/00-cohttp_lwt.ml index 5a98a950b9..a1e31adbc7 100644 --- a/data/cookbook/http-rest/00-cohttp_lwt.ml +++ b/data/cookbook/http-rest/00-cohttp_lwt.ml @@ -33,17 +33,17 @@ open Cohttp_lwt_unix sure to review the API's documentation on what it expects to receive*) let message_body = ref "{\"body\":\"This is a good issue to work on!\"}" -(* `request_body` contains our API endpoint `uri`. This example uses the Github issue comments -api endpoint. You will need to fill in ``, ``, and `` with -your own information from Github.*) +(* `request_body` contains our API endpoint `uri`. This example uses the GitHub issue comments +API endpoint. You will need to fill in ``, ``, and `` with +your own information from GitHub.*) let request_body = let uri = Uri.of_string "https://api.github.com/repos///issues//comments" in (* define headers using `Header.init` and `Header.add`. Since we are using Bearer Token authentication, - we need to add the `Authorization` header which has the form `Bearer `. Please ensure you always - keep your token secret. You can refer to the `read-environment-variable` section in the cookbook for one way to protect + we need to add the `Authorization` header, which has the form `Bearer `. Please ensure you always + keep your token secret. You can refer to the `read-environment-variable` section in the cookbook for one way to protect your token*) let headers = Header.init () @@ -53,9 +53,9 @@ let request_body = Header.add h "Authorization" "Bearer " |> fun h -> Header.add h "X-GitHub-Api-Version" "2022-11-28" in - (* initialize the `body` to contain the json formatted text defined earlier in `message_body`. We tell the client + (* initialise the `body` to contain the JSON formatted text defined earlier in `message_body`. We tell the client we are making a `POST` call and pass the headers and body. `Client.call` performs the operation - and returns a `response` and a `code` to let us know if our call worked as expected (i.e. 201 means the call created a resource). *) + and returns a `response` and a `code` to let us know if our call worked as expected (i.e., 201 means the call created a resource). *) let body = Cohttp_lwt.Body.of_string !message_body in Client.call ~headers ~body `POST uri >>= fun (response, body) -> From 9482c8eecbaf571a414b8b0cf33fa19fc5ee33b0 Mon Sep 17 00:00:00 2001 From: ggsmith842 Date: Fri, 21 Jun 2024 18:54:53 -0600 Subject: [PATCH 06/17] formatting discussion for tls error --- data/cookbook/http-rest/00-cohttp_lwt.ml | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/data/cookbook/http-rest/00-cohttp_lwt.ml b/data/cookbook/http-rest/00-cohttp_lwt.ml index a1e31adbc7..28e2c74eaf 100644 --- a/data/cookbook/http-rest/00-cohttp_lwt.ml +++ b/data/cookbook/http-rest/00-cohttp_lwt.ml @@ -12,16 +12,11 @@ packages: tested_version: "5.3.1" used_libraries: - cohttp-lwt-unix -- name: "tls" - tested_version: "0.17.5" - name: "tls-lwt" tested_version: "0.17.5" -- name: "ssl" - tested_version: "0.7.0" discussion: | - **REST API Operations - POST:** A `POST` operation creates a *new* resouce. Ex. Add data to a database. - - **SSL/TLS Note:** Running this example may result in an error: `Exception: Failure “No SSL or TLS support compiled into Conduit`. - To resolve this issue you can run `opam install ssl tls-lwt` + - **SSL/TLS Note:** Running this example may result in an error: `Exception: Failure “No SSL or TLS support compiled into Conduit`. To resolve this issue you can run `opam install tls-lwt` - **The code below uses the GitHub REST API as an example. Please review the documentation here: [github.com/restap/issues/comments](https://docs.github.com/en/rest/issues/comments?apiVersion=2022-11-28#create-an-issue-comment) --- @@ -61,7 +56,7 @@ let request_body = >>= fun (response, body) -> let code = response |> Response.status |> Code.code_of_status in body |> Cohttp_lwt.Body.to_string >|= fun body -> code, body -;; + (* `Lwt_main.run` executes our call defined in the `request_body` and then we print the `response_code` and the `response_body` *) @@ -69,7 +64,7 @@ let () = let response_code, response_body = Lwt_main.run request_body in Printf.printf "Respose code: %d\n" response_code; Printf.printf "Response body: %s\n" response_body -;; + From cd945c001ba3c3ee5f6295710f39a8e38c4464a2 Mon Sep 17 00:00:00 2001 From: ggsmith842 Date: Fri, 21 Jun 2024 19:08:39 -0600 Subject: [PATCH 07/17] rename folder --- .../00-cohttp_lwt.ml | 4 ---- 1 file changed, 4 deletions(-) rename data/cookbook/{http-rest => make-http-post-request-bearer-auth}/00-cohttp_lwt.ml (99%) diff --git a/data/cookbook/http-rest/00-cohttp_lwt.ml b/data/cookbook/make-http-post-request-bearer-auth/00-cohttp_lwt.ml similarity index 99% rename from data/cookbook/http-rest/00-cohttp_lwt.ml rename to data/cookbook/make-http-post-request-bearer-auth/00-cohttp_lwt.ml index 28e2c74eaf..7fe1ba8feb 100644 --- a/data/cookbook/http-rest/00-cohttp_lwt.ml +++ b/data/cookbook/make-http-post-request-bearer-auth/00-cohttp_lwt.ml @@ -64,7 +64,3 @@ let () = let response_code, response_body = Lwt_main.run request_body in Printf.printf "Respose code: %d\n" response_code; Printf.printf "Response body: %s\n" response_body - - - - From 055c8b7d7e9049634e8871bdcea86a5c8b911c44 Mon Sep 17 00:00:00 2001 From: ggsmith842 Date: Fri, 21 Jun 2024 19:51:56 -0600 Subject: [PATCH 08/17] combine comments --- .../00-cohttp_lwt.ml | 28 +++++++++++-------- 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/data/cookbook/make-http-post-request-bearer-auth/00-cohttp_lwt.ml b/data/cookbook/make-http-post-request-bearer-auth/00-cohttp_lwt.ml index 7fe1ba8feb..2485e1f393 100644 --- a/data/cookbook/make-http-post-request-bearer-auth/00-cohttp_lwt.ml +++ b/data/cookbook/make-http-post-request-bearer-auth/00-cohttp_lwt.ml @@ -15,9 +15,9 @@ packages: - name: "tls-lwt" tested_version: "0.17.5" discussion: | - - **REST API Operations - POST:** A `POST` operation creates a *new* resouce. Ex. Add data to a database. - - **SSL/TLS Note:** Running this example may result in an error: `Exception: Failure “No SSL or TLS support compiled into Conduit`. To resolve this issue you can run `opam install tls-lwt` - - **The code below uses the GitHub REST API as an example. Please review the documentation here: [github.com/restap/issues/comments](https://docs.github.com/en/rest/issues/comments?apiVersion=2022-11-28#create-an-issue-comment) + - **REST API Operations - POST:** A `POST` operation creates a *new* resouce. Example: Add data to a database + - **SSL-TLS Exception:** Running this example may result in an error: `Exception: Failure “No SSL or TLS support compiled into Conduit`. To resolve this issue you can run `opam install tls-lwt` + - **Reference:** The code below uses the GitHub REST API as an example. Please review the documentation here: [github.com/restap/issues/comments](https://docs.github.com/en/rest/issues/comments?apiVersion=2022-11-28#create-an-issue-comment) --- open Lwt @@ -28,18 +28,25 @@ open Cohttp_lwt_unix sure to review the API's documentation on what it expects to receive*) let message_body = ref "{\"body\":\"This is a good issue to work on!\"}" -(* `request_body` contains our API endpoint `uri`. This example uses the GitHub issue comments +(* + +`request_body` contains our API endpoint `uri`. This example uses the GitHub issue comments API endpoint. You will need to fill in ``, ``, and `` with -your own information from GitHub.*) +your own information from GitHub. + +Define headers using `Header.init` and `Header.add`. Since we are using Bearer Token authentication, we need to add the `Authorization` header, which has the form `Bearer `. + +Ensure you always keep your token secret. You can refer to the `read-environment-variable` section in the cookbook for one way to protect your token. + +Initialise the `body` to contain the JSON formatted text defined earlier in `message_body`. We tell the client we are making a `POST` call and pass the headers and body. +`Client.call` performs the operation and returns a `response` and a `code` to let us know if our call worked as expected (i.e., 201 means the call created a resource). + +*) let request_body = let uri = Uri.of_string "https://api.github.com/repos///issues//comments" in - (* define headers using `Header.init` and `Header.add`. Since we are using Bearer Token authentication, - we need to add the `Authorization` header, which has the form `Bearer `. Please ensure you always - keep your token secret. You can refer to the `read-environment-variable` section in the cookbook for one way to protect - your token*) let headers = Header.init () |> fun h -> @@ -48,9 +55,6 @@ let request_body = Header.add h "Authorization" "Bearer " |> fun h -> Header.add h "X-GitHub-Api-Version" "2022-11-28" in - (* initialise the `body` to contain the JSON formatted text defined earlier in `message_body`. We tell the client - we are making a `POST` call and pass the headers and body. `Client.call` performs the operation - and returns a `response` and a `code` to let us know if our call worked as expected (i.e., 201 means the call created a resource). *) let body = Cohttp_lwt.Body.of_string !message_body in Client.call ~headers ~body `POST uri >>= fun (response, body) -> From c2ee560f445a3856d05cb03a8f11f68c6335132f Mon Sep 17 00:00:00 2001 From: ggsmith842 Date: Fri, 21 Jun 2024 20:09:39 -0600 Subject: [PATCH 09/17] removed used libraries --- .../make-http-post-request-bearer-auth/00-cohttp_lwt.ml | 6 ------ 1 file changed, 6 deletions(-) diff --git a/data/cookbook/make-http-post-request-bearer-auth/00-cohttp_lwt.ml b/data/cookbook/make-http-post-request-bearer-auth/00-cohttp_lwt.ml index 2485e1f393..66431bd344 100644 --- a/data/cookbook/make-http-post-request-bearer-auth/00-cohttp_lwt.ml +++ b/data/cookbook/make-http-post-request-bearer-auth/00-cohttp_lwt.ml @@ -2,16 +2,10 @@ packages: - name: "lwt" tested_version: "5.7.0" - used_libraries: - - lwt - name: "cohttp" tested_version: "5.3.1" - used_libraries: - - cohttp - name: "cohttp-lwt-unix" tested_version: "5.3.1" - used_libraries: - - cohttp-lwt-unix - name: "tls-lwt" tested_version: "0.17.5" discussion: | From 7ac0900ed569d0ae643493cee8407868f3b085d9 Mon Sep 17 00:00:00 2001 From: ggsmith842 Date: Fri, 21 Jun 2024 20:25:09 -0600 Subject: [PATCH 10/17] reformat yaml --- .../00-cohttp_lwt.ml | 26 ++++++++++++------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/data/cookbook/make-http-post-request-bearer-auth/00-cohttp_lwt.ml b/data/cookbook/make-http-post-request-bearer-auth/00-cohttp_lwt.ml index 66431bd344..8c8bf95cff 100644 --- a/data/cookbook/make-http-post-request-bearer-auth/00-cohttp_lwt.ml +++ b/data/cookbook/make-http-post-request-bearer-auth/00-cohttp_lwt.ml @@ -1,16 +1,24 @@ --- packages: -- name: "lwt" - tested_version: "5.7.0" -- name: "cohttp" - tested_version: "5.3.1" -- name: "cohttp-lwt-unix" - tested_version: "5.3.1" -- name: "tls-lwt" - tested_version: "0.17.5" + - name: "lwt" + tested_version: "5.7.0" + used_libraries: + - lwt + - name: "cohttp" + tested_version: "5.3.1" + used_libraries: + - cohttp + - name: "cohttp-lwt-unix" + tested_version: "5.3.1" + used_libraries: + - cohttp-lwt-unix + - name: "tls-lwt" + tested_version: "0.17.5" + used_libraries: + - tls-lwt discussion: | - **REST API Operations - POST:** A `POST` operation creates a *new* resouce. Example: Add data to a database - - **SSL-TLS Exception:** Running this example may result in an error: `Exception: Failure “No SSL or TLS support compiled into Conduit`. To resolve this issue you can run `opam install tls-lwt` + - **SSL-TLS Exception:** Running this example may result in an error: `Exception: Failure No SSL or TLS support compiled into Conduit`. To resolve this issue you can run `opam install tls-lwt` - **Reference:** The code below uses the GitHub REST API as an example. Please review the documentation here: [github.com/restap/issues/comments](https://docs.github.com/en/rest/issues/comments?apiVersion=2022-11-28#create-an-issue-comment) --- From a4c8e67a89d0852c1e79365e851740ae29a75b41 Mon Sep 17 00:00:00 2001 From: ggsmith842 Date: Sat, 22 Jun 2024 17:32:14 -0600 Subject: [PATCH 11/17] add PATCH operation example --- .../00-cohttp_lwt.ml | 72 +++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 data/cookbook/make-http-patch-request-bearer-auth/00-cohttp_lwt.ml diff --git a/data/cookbook/make-http-patch-request-bearer-auth/00-cohttp_lwt.ml b/data/cookbook/make-http-patch-request-bearer-auth/00-cohttp_lwt.ml new file mode 100644 index 0000000000..3c52cbdd98 --- /dev/null +++ b/data/cookbook/make-http-patch-request-bearer-auth/00-cohttp_lwt.ml @@ -0,0 +1,72 @@ +--- +packages: + - name: "lwt" + tested_version: "5.7.0" + used_libraries: + - lwt + - name: "cohttp" + tested_version: "5.3.1" + used_libraries: + - cohttp + - name: "cohttp-lwt-unix" + tested_version: "5.3.1" + used_libraries: + - cohttp-lwt-unix + - name: "tls-lwt" + tested_version: "0.17.5" + used_libraries: + - tls-lwt +discussion: | + - **REST API Operations - PATCH:** A `PATCH` operation updates an *existing* resouce. Example: Change an account balance for a customer + - **SSL-TLS Exception:** Running this example may result in an error: `Exception: Failure No SSL or TLS support compiled into Conduit`. To resolve this issue you can run `opam install tls-lwt` + - **Reference:** The code below uses the GitHub REST API as an example. Please review the documentation here: [github.com/restap/issues/comments](https://docs.github.com/en/rest/issues/comments?apiVersion=2022-11-28#create-an-issue-comment) +--- + +open Lwt +open Cohttp +open Cohttp_lwt_unix + +(* create the message payload for our PATCH request. This can vary from API to API so be +sure to review the API's documentation on what it expects to receive*) +let message_body = ref "{\"body\":\"Updated with the Github REST API!\"}" + +(* + +`request_body` contains our API endpoint `uri`. This example uses the GitHub issue comments +API endpoint. You will need to fill in ``, ``, and `` with +your own information from GitHub. + +Define headers using `Header.init` and `Header.add`. Since we are using Bearer Token authentication, we need to add the `Authorization` header, which has the form `Bearer `. + +Ensure you always keep your token secret. You can refer to the `read-environment-variable` section in the cookbook for one way to protect your token. + +Initialise the `body` to contain the JSON formatted text defined earlier in `message_body`. We tell the client we are making a `PATCH` call and pass the headers and body. +`Client.call` performs the operation and returns a `response` and a `code` to let us know if our call worked as expected (i.e., 201 means the call created a resource). + +If you have already read the POST cookbook recipe, you will notice we only needed to change the `POST` to `PATCH` in our code. +*) +let request_body = + let uri = + Uri.of_string "https://api.github.com/repos///issues/comments/" + in + let headers = + Header.init () + |> fun h -> + Header.add h "Accept" "application/vnd.github+json" + |> fun h -> + Header.add h "Authorization" "Bearer " + |> fun h -> Header.add h "X-GitHub-Api-Version" "2022-11-28" + in + let body = Cohttp_lwt.Body.of_string !message_body in + Client.call ~headers ~body `PATCH uri + >>= fun (response, body) -> + let code = response |> Response.status |> Code.code_of_status in + body |> Cohttp_lwt.Body.to_string >|= fun body -> code, body + + +(* `Lwt_main.run` executes our call defined in the `request_body` and then + we print the `response_code` and the `response_body` *) +let () = + let response_code, response_body = Lwt_main.run request_body in + Printf.printf "Respose code: %d\n" response_code; + Printf.printf "Response body: %s\n" response_body From 0204c528021889ba94fdd32a268d853b3c3d9475 Mon Sep 17 00:00:00 2001 From: ggsmith842 Date: Sat, 22 Jun 2024 17:37:31 -0600 Subject: [PATCH 12/17] update task.yml to include PATCH recipe --- data/cookbook/tasks.yml | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/data/cookbook/tasks.yml b/data/cookbook/tasks.yml index 75bb6a5dc7..83fc672f55 100644 --- a/data/cookbook/tasks.yml +++ b/data/cookbook/tasks.yml @@ -230,10 +230,14 @@ categories: slug: download-file-to-temporary-dir - title: Make a Partial Download with HTTP Range Header slug: make-partial-download-with-http-range-header - - title: Make a HTTP POST Request with Bearer Token Authentication + - title: Make an HTTP POST Request with Bearer Token Authentication slug: make-http-post-request-bearer-auth description: > - Make an HTTP POST request then print response code and body + Make an HTTP POST request to create a resource then print response code and body + - title: Make an HTTP PATCH Request with Bearer Token Authentication + slug: make-http-patch-request-bearer-auth + description: > + Make an HTTP PATCH request to update a resource then print response code and body - title: Dealing with HTML tasks: - title: Render a HTML Template From e753d7534a587e6368f283032d14cecd092853ca Mon Sep 17 00:00:00 2001 From: Grant Smith <57376089+ggsmith842@users.noreply.github.com> Date: Thu, 27 Jun 2024 13:32:57 -0600 Subject: [PATCH 13/17] Apply suggestions from code review Co-authored-by: Cuihtlauac Alvarado --- .../make-http-post-request-bearer-auth/00-cohttp_lwt.ml | 2 +- data/cookbook/tasks.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/data/cookbook/make-http-post-request-bearer-auth/00-cohttp_lwt.ml b/data/cookbook/make-http-post-request-bearer-auth/00-cohttp_lwt.ml index 8c8bf95cff..8e9c43f093 100644 --- a/data/cookbook/make-http-post-request-bearer-auth/00-cohttp_lwt.ml +++ b/data/cookbook/make-http-post-request-bearer-auth/00-cohttp_lwt.ml @@ -26,7 +26,7 @@ open Lwt open Cohttp open Cohttp_lwt_unix -(* create the message payload for our POST request. This can vary from API to API so be +(* Create POST request message payload. This can vary from API to API so be sure to review the API's documentation on what it expects to receive*) let message_body = ref "{\"body\":\"This is a good issue to work on!\"}" diff --git a/data/cookbook/tasks.yml b/data/cookbook/tasks.yml index 83fc672f55..3d7a049590 100644 --- a/data/cookbook/tasks.yml +++ b/data/cookbook/tasks.yml @@ -233,7 +233,7 @@ categories: - title: Make an HTTP POST Request with Bearer Token Authentication slug: make-http-post-request-bearer-auth description: > - Make an HTTP POST request to create a resource then print response code and body + Make an HTTP POST request to create a resource - title: Make an HTTP PATCH Request with Bearer Token Authentication slug: make-http-patch-request-bearer-auth description: > From 4eae96c1a8dc377e378962a87f4ca927453ae87e Mon Sep 17 00:00:00 2001 From: Grant Smith <57376089+ggsmith842@users.noreply.github.com> Date: Thu, 27 Jun 2024 13:34:05 -0600 Subject: [PATCH 14/17] Update tasks.yml --- data/cookbook/tasks.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data/cookbook/tasks.yml b/data/cookbook/tasks.yml index 3d7a049590..030a521d0e 100644 --- a/data/cookbook/tasks.yml +++ b/data/cookbook/tasks.yml @@ -237,7 +237,7 @@ categories: - title: Make an HTTP PATCH Request with Bearer Token Authentication slug: make-http-patch-request-bearer-auth description: > - Make an HTTP PATCH request to update a resource then print response code and body + Make an HTTP PATCH request to update a resource - title: Dealing with HTML tasks: - title: Render a HTML Template From 993350758ca24daa8e04ea0bd90e98018ed5a6ce Mon Sep 17 00:00:00 2001 From: Grant Smith <57376089+ggsmith842@users.noreply.github.com> Date: Thu, 27 Jun 2024 15:01:29 -0600 Subject: [PATCH 15/17] Improve readability 00-cohttp_lwt.ml --- .../00-cohttp_lwt.ml | 47 +++++++------------ 1 file changed, 18 insertions(+), 29 deletions(-) diff --git a/data/cookbook/make-http-patch-request-bearer-auth/00-cohttp_lwt.ml b/data/cookbook/make-http-patch-request-bearer-auth/00-cohttp_lwt.ml index 3c52cbdd98..d670a1f1c1 100644 --- a/data/cookbook/make-http-patch-request-bearer-auth/00-cohttp_lwt.ml +++ b/data/cookbook/make-http-patch-request-bearer-auth/00-cohttp_lwt.ml @@ -4,6 +4,10 @@ packages: tested_version: "5.7.0" used_libraries: - lwt + - name: "lwt_ppx" + tested_version: "5.7.0" + used_libraries: + - lwt_ppx - name: "cohttp" tested_version: "5.3.1" used_libraries: @@ -16,56 +20,41 @@ packages: tested_version: "0.17.5" used_libraries: - tls-lwt -discussion: | - - **REST API Operations - PATCH:** A `PATCH` operation updates an *existing* resouce. Example: Change an account balance for a customer +discussion: | - **SSL-TLS Exception:** Running this example may result in an error: `Exception: Failure No SSL or TLS support compiled into Conduit`. To resolve this issue you can run `opam install tls-lwt` - **Reference:** The code below uses the GitHub REST API as an example. Please review the documentation here: [github.com/restap/issues/comments](https://docs.github.com/en/rest/issues/comments?apiVersion=2022-11-28#create-an-issue-comment) --- -open Lwt +open Lwt.Syntax open Cohttp open Cohttp_lwt_unix -(* create the message payload for our PATCH request. This can vary from API to API so be -sure to review the API's documentation on what it expects to receive*) +(* create the message payload for our PATCH request.*) let message_body = ref "{\"body\":\"Updated with the Github REST API!\"}" (* - -`request_body` contains our API endpoint `uri`. This example uses the GitHub issue comments -API endpoint. You will need to fill in ``, ``, and `` with -your own information from GitHub. - -Define headers using `Header.init` and `Header.add`. Since we are using Bearer Token authentication, we need to add the `Authorization` header, which has the form `Bearer `. - -Ensure you always keep your token secret. You can refer to the `read-environment-variable` section in the cookbook for one way to protect your token. - -Initialise the `body` to contain the JSON formatted text defined earlier in `message_body`. We tell the client we are making a `PATCH` call and pass the headers and body. -`Client.call` performs the operation and returns a `response` and a `code` to let us know if our call worked as expected (i.e., 201 means the call created a resource). - -If you have already read the POST cookbook recipe, you will notice we only needed to change the `POST` to `PATCH` in our code. +Since we are using Bearer Token authentication, we need to add the `Authorization` header, which has the form `Bearer `. +Ensure you always keep your token secret. *) let request_body = let uri = Uri.of_string "https://api.github.com/repos///issues/comments/" in + let header_add s1 s2 h = Header.add h s1 s2 in let headers = Header.init () - |> fun h -> - Header.add h "Accept" "application/vnd.github+json" - |> fun h -> - Header.add h "Authorization" "Bearer " - |> fun h -> Header.add h "X-GitHub-Api-Version" "2022-11-28" + |> header_add "Accept" "application/vnd.github+json" + |> header_add "Authorization" "Bearer " + |> header_add "X-GitHub-Api-Version" "2022-11-28" in let body = Cohttp_lwt.Body.of_string !message_body in - Client.call ~headers ~body `PATCH uri - >>= fun (response, body) -> - let code = response |> Response.status |> Code.code_of_status in - body |> Cohttp_lwt.Body.to_string >|= fun body -> code, body + let* resp, resp_body = Client.call ~headers ~body `PATCH uri in + let code = resp |> Response.status |> Code.code_of_status in + let+ body = Cohttp_lwt.Body.to_string resp_body in + code, body -(* `Lwt_main.run` executes our call defined in the `request_body` and then - we print the `response_code` and the `response_body` *) +(* Example usage to print response code and response body *) let () = let response_code, response_body = Lwt_main.run request_body in Printf.printf "Respose code: %d\n" response_code; From 79b44033f7c64cef719725685b81a5c62fdde239 Mon Sep 17 00:00:00 2001 From: Grant Smith <57376089+ggsmith842@users.noreply.github.com> Date: Thu, 27 Jun 2024 15:23:47 -0600 Subject: [PATCH 16/17] Improve readability 00-cohttp_lwt.ml --- .../00-cohttp_lwt.ml | 58 +++++++++---------- 1 file changed, 26 insertions(+), 32 deletions(-) diff --git a/data/cookbook/make-http-post-request-bearer-auth/00-cohttp_lwt.ml b/data/cookbook/make-http-post-request-bearer-auth/00-cohttp_lwt.ml index 8e9c43f093..d1a713c7ac 100644 --- a/data/cookbook/make-http-post-request-bearer-auth/00-cohttp_lwt.ml +++ b/data/cookbook/make-http-post-request-bearer-auth/00-cohttp_lwt.ml @@ -4,6 +4,10 @@ packages: tested_version: "5.7.0" used_libraries: - lwt + - name: "lwt_ppx" + tested_version: "2.1.0" + used_libraries: + - lwt_ppx - name: "cohttp" tested_version: "5.3.1" used_libraries: @@ -22,50 +26,40 @@ discussion: | - **Reference:** The code below uses the GitHub REST API as an example. Please review the documentation here: [github.com/restap/issues/comments](https://docs.github.com/en/rest/issues/comments?apiVersion=2022-11-28#create-an-issue-comment) --- -open Lwt +open Lwt.Syntax open Cohttp open Cohttp_lwt_unix -(* Create POST request message payload. This can vary from API to API so be -sure to review the API's documentation on what it expects to receive*) +(* Create POST request message payload in JSON format. *) let message_body = ref "{\"body\":\"This is a good issue to work on!\"}" (* +`request_body` contains the API endpoint `uri` -`request_body` contains our API endpoint `uri`. This example uses the GitHub issue comments -API endpoint. You will need to fill in ``, ``, and `` with -your own information from GitHub. - -Define headers using `Header.init` and `Header.add`. Since we are using Bearer Token authentication, we need to add the `Authorization` header, which has the form `Bearer `. - -Ensure you always keep your token secret. You can refer to the `read-environment-variable` section in the cookbook for one way to protect your token. - -Initialise the `body` to contain the JSON formatted text defined earlier in `message_body`. We tell the client we are making a `POST` call and pass the headers and body. -`Client.call` performs the operation and returns a `response` and a `code` to let us know if our call worked as expected (i.e., 201 means the call created a resource). +Since we are using Bearer Token authentication, we need to add the `Authorization` header, which has the form `Bearer `. +Ensure you always keep your token secret. +In `Client.call` we define the operation to be a `POST` and send `body` as the message payload. *) + let request_body = - let uri = - Uri.of_string - "https://api.github.com/repos///issues//comments" - in - let headers = - Header.init () - |> fun h -> - Header.add h "Accept" "application/vnd.github+json" - |> fun h -> - Header.add h "Authorization" "Bearer " - |> fun h -> Header.add h "X-GitHub-Api-Version" "2022-11-28" + let uri = Uri.of_string "https://api.github.com/repos///issues//comments" in - let body = Cohttp_lwt.Body.of_string !message_body in - Client.call ~headers ~body `POST uri - >>= fun (response, body) -> - let code = response |> Response.status |> Code.code_of_status in - body |> Cohttp_lwt.Body.to_string >|= fun body -> code, body - + let header_add s1 s2 h = Header.add h s1 s2 in + let headers = + Header.init () + |> header_add "Accept" "application/vnd.github+json" + |> header_add "Authorization" "Bearer " + |> header_add "X-GitHub-Api-Version" "2022-11-28" + in + let body = Cohttp_lwt.Body.of_string !message_body in + + let* resp, resp_body = Client.call ~headers ~body `POST uri in + let code = resp |> Response.status |> Code.code_of_status in + let+ resp_body = Cohttp_lwt.Body.to_string resp_body in + code, resp_body -(* `Lwt_main.run` executes our call defined in the `request_body` and then - we print the `response_code` and the `response_body` *) +(* Example usage *) let () = let response_code, response_body = Lwt_main.run request_body in Printf.printf "Respose code: %d\n" response_code; From 9eb7288b86780a1529dd3c71da58a253b31d4c6c Mon Sep 17 00:00:00 2001 From: Grant Smith <57376089+ggsmith842@users.noreply.github.com> Date: Thu, 27 Jun 2024 15:25:11 -0600 Subject: [PATCH 17/17] Fix lwt_ppx version --- .../make-http-patch-request-bearer-auth/00-cohttp_lwt.ml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data/cookbook/make-http-patch-request-bearer-auth/00-cohttp_lwt.ml b/data/cookbook/make-http-patch-request-bearer-auth/00-cohttp_lwt.ml index d670a1f1c1..2e136087f5 100644 --- a/data/cookbook/make-http-patch-request-bearer-auth/00-cohttp_lwt.ml +++ b/data/cookbook/make-http-patch-request-bearer-auth/00-cohttp_lwt.ml @@ -5,7 +5,7 @@ packages: used_libraries: - lwt - name: "lwt_ppx" - tested_version: "5.7.0" + tested_version: "2.1.0" used_libraries: - lwt_ppx - name: "cohttp"