- Based on Nim std/httpclient
- No additional dependencies
- API focused on DX
nimble install yahttpmore examples here
import yahttp
echo get("https://www.google.com/").statusimport json
import yahttp
let laptopsJson = get("https://dummyjson.com/products/search", query = {"q": "Laptop"}).json()
echo laptopsJson["products"][0]["title"].getStr()get("http://api")
put("http://api")
post("http://api")
patch("http://api")
delete("http://api")
head("http://api")
options("http://api")Arguments:
url- request URL. The only required argumentheaders- request HTTP headers. Example:{"header1": "val", "header2": "val2"}query- request query params. Example:{"param1": "val", "param2": "val2"}encodeQueryParams- parameters forencodeQueryfunction that encodes query params. Morebody- request body as a string. Example:"{\"key\": \"value\"}". Is not available forget,headandoptionsproceduresfiles- array of files to upload. Every file is a tuple of multipart name, file name, content type and contentsreamingFiles- array of files to stream from disc and upload. Every file is a tuple of multipart name and file pathauth- login and password for basic authorization. Example:("login", "password")timeout- stop waiting for a response after a given number of milliseconds.-1for no timeout, which is default valueignoreSsl- no certificate verification iftruesslContext- SSL context for TLS/SSL connections. See newContext
request("http://api")Has the same arguments as method procedures and one additional:
httpMethod- HTTP method.Method.GETby default. Example:Method.POST
All procedures above return Response object with fields:
status- HTTP status codebody- response body as a stringheaders- table, where keys are header keys and values are sequences of header values for a keyrequest- object with request data processed byyahttpurl- stores full url with query paramsheaders- stores HTTP headers withAuthorizationfor basic authorizationhttpMethod- HTTP methodbody- request body as a string
Response object has some helper procedures:
Response.json()- returns response body as JSONResponse.html()- returns response body as HTMLResponse.to(t)- converts response body to JSON and unmarshals it to typetResponse.ok()- returnstrueifstatusis greater than 0 and less than 400Response.raiseForStatus()- throwsHttpErrorexceptions if status is 400 or above
object.toJsonString() - converts object of any type to json string. Helpful to use for body argument