Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cookbook Send a POST/PATCH Request w/ Authentication #2534

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
---
packages:
- name: "lwt"
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:
- 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: |
- **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.Syntax
open Cohttp
open Cohttp_lwt_unix

(* create the message payload for our PATCH request.*)
let message_body = ref "{\"body\":\"Updated with the Github REST API!\"}"

(*
Since we are using Bearer Token authentication, we need to add the `Authorization` header, which has the form `Bearer <your token>`.
Ensure you always keep your token secret.
*)
let request_body =
let uri =
Uri.of_string "https://api.github.com/repos/<username>/<repo>/issues/comments/<comment_id>"
in
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 <your token here>"
|> 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 `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

(* 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;
Printf.printf "Response body: %s\n" response_body
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
---
packages:
- name: "lwt"
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:
- 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`
- **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.Syntax
open Cohttp
open Cohttp_lwt_unix

(* 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`

Since we are using Bearer Token authentication, we need to add the `Authorization` header, which has the form `Bearer <your token>`.
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/<username>/<repo>/issues/<issue number>/comments"
in
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 <your token here>"
|> 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

(* Example usage *)
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
10 changes: 9 additions & 1 deletion data/cookbook/tasks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -224,12 +224,20 @@ 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
- title: Make a Partial Download with HTTP Range Header
slug: make-partial-download-with-http-range-header
- 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
- 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
- title: Dealing with HTML
tasks:
- title: Render a HTML Template
Expand Down
Loading