Skip to content

Instantly share code, notes, and snippets.

@peheje
Created August 30, 2023 19:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peheje/baa20a7b3e4e01732934825318f9ecfd to your computer and use it in GitHub Desktop.
Save peheje/baa20a7b3e4e01732934825318f9ecfd to your computer and use it in GitHub Desktop.
Few concepts in Nim in few lines of code
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