exsnake exsnake - 1 month ago 9
HTTP Question

HttpServer and Julia error with match method

I'm trying to understand how to work with this package for Julia.

Im using the following code (is an example from the package):

using HttpServer

function fibonacci(n)
if n == 1 return 1 end
if n == 2 return 1 end
prev = BigInt(1)
pprev = BigInt(1)
for i=3:n
curr = prev + pprev
pprev = prev
prev = curr
end
return prev
end

http = HttpHandler() do req::Request, res::Response
m = match(r"^/fibo/(\d+)/?$",req.resource)
if m == nothing
return Response(404)
end
number = BigInt(m.captures[1])
if number < 1 || number > 100_000
return Response(500)
end
return Response(string(fibonacci(number)))
end

http.events["error"] = (client, err) -> println(err)
http.events["listen"] = (port) -> println("Listening on $port...")

server = Server(http)
run(server, 8031)


And trying to access to the server with this link:


http://localhost:8031/fibo/100


But i get the next error:


MethodError(convert,(BigInt,"100"))

ERROR: MethodError: Cannot
convert
an object of type
SubString{String} to an object of type BigInt


What im doing wrong?

I have problems to figure out what
r"^/fibo/(\d+)/?
does, maybe there is my problem...

Answer

You get this error because method BigInt(s::AbstractString) is deprecated and was remove in julia 0.5. Use number = parse(BigInt,m.captures[1]) instead.

Comments