four ways of the same program, one ends in an error. Why? I was surprised and unable to find out the cause.
#!/usr/bin/env bash
echo "compile with full url, will be OK"
nim c -r -d:ssl --define:http ./devel.nim
echo "compile with url and set scheme, will be ERROR"
nim c -r -d:ssl --define:scheme ./devel.nim
echo "with with full url , request url AS STRING, will be OK"
nim c -r -d:ssl --define:http --define:str ./devel.nim
echo "compile with url and set scheme, request url AS STRING, will be OK"
nim c -r -d:ssl --define:scheme --define:str ./devel.nim
import std/httpcore
import std/uri
import std/httpclient
when defined http:
var url = parseUri "https://nim-lang.org"
when defined scheme:
var url = parseUri "nim-lang.org"
url.scheme = "http"
echo "url is ", $url
let metoda = HttpGet
var client = newHttpClient()
try:
when defined str:
let response = client.request(url = $url, httpMethod = metoda)
else:
let response = client.request(url = url, httpMethod = metoda)
echo response.status
finally:
client.close()
For http and https it makes the same error.
At first I thought the problem was on my domain, which I was connecting to in my code. I ruled that out. You can change the domain in that example,. I've tried for example google.com and it makes the same error.
It's not a problem, as far as I know. Just convert the url to a string
client.request(url = $url, httpMethod = method)
and the mysterious error will disappear.