Created
August 30, 2023 19:12
-
-
Save peheje/baa20a7b3e4e01732934825318f9ecfd to your computer and use it in GitHub Desktop.
Few concepts in Nim in few lines of code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import std/httpclient | |
import std/strutils | |
import std/json | |
import std/random | |
proc toJson(o: auto): string = | |
result = $(%*o) | |
type | |
MyBody = object | |
id: string | |
MyBody2 = object | |
id: int | |
const authToken = "your-authorization-token" | |
let ids = readFile("ids.txt").splitLines | |
let headers = newHttpHeaders([ | |
("Authorization", "Bearer " & authToken), | |
("User-Agent", "") | |
]) | |
let client = newHttpClient(headers = headers) | |
for i, id in ids: | |
let body = | |
if rand(1) == 0: | |
toJson(MyBody(id:id)) | |
else: | |
toJson(MyBody2(id:i)) | |
let response = client.post("https://httpbin.org/post", body=body) | |
if response.status == "200 OK": | |
echo "ID: ", id, " - POST succeeded with status code 200. Body: " & response.body | |
else: | |
echo "ID: ", id, " - POST failed with status code ", response.status |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment