Search code examples
httpauthenticationposttokencommon-lisp

Why is Common Lisp famous library Dexador not working for this HTTP POST request case? Is it a bug or did I miss something?


I am using SBCL, Emacs, Slime, and Dexador. I am also watching this course on Udemy about Postman.

At some point, the instructor presents a POST request that uses basic authentication. It is summarized by the following picture:

enter image description here

The POST request is made at the address https://simple-tool-rental-api.glitch.me/api-clients. In addition, a message as the body in JSON format is submitted:

{
    "clientName": "Postman",
    "clientEmail": "[email protected]"
}

As a reply, the server responds with:

{
    "accessToken": "079e7bb6d3832c0d7054ae0e1146d6ed3277836fefd2e0aa9e5d7d207945e17f"
}

I tried doing the same using the famous library called Dexador and using the documentation instructions:

CL-USER>(dex:post "https://simple-tool-rental-api.glitch.me/api-clients"
            :content '(("clientName" . "Postman") ("clientEmail" . "[email protected]")))

Unfortunately, Slime returns an error message indicating a 400 bad request:

An HTTP request to "https://simple-tool-rental-api.glitch.me/api-clients" returned 400 bad request.

{"error":"Invalid or missing client name."}

Is this a bug? Did I miss something?


Solution

  • A working example:

    (dex:request "https://simple-tool-rental-api.glitch.me/api-clients"
                          :method :post 
                          :headers '(("Content-Type" . "application/json"))
                          :content "{\"clientName\":\"Postman\",
                            \"clientEmail\": \"[email protected]\"}" 
                          :verbose t)
    >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    POST /api-clients HTTP/1.1
    User-Agent: Dexador/0.9.15 (SBCL 1.4.5.debian); Linux; 5.0.0-31-generic
    Host: simple-tool-rental-api.glitch.me
    Accept: */*
    Content-Type: application/json
    Content-Length: 83
    
    >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    HTTP/1.1 201 Created
    Date: Tue, 05 Apr 2022 12:44:55 GMT
    Content-Type: application/json; charset=utf-8
    Content-Length: 82
    Connection: keep-alive
    x-powered-by: Express
    etag: W/"52-egRRCktqEOIEh9dS2uY8rWUiyfY"
    
    <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    "{\"accessToken\":\"7e559ebee8ff889054346dbbd727c1bb9b9d55123fb5bd49686337e8829f8b78\"}"
    201
    #<HASH-TABLE :TEST EQUAL :COUNT 6 {1003C05373}>
    #<QURI.URI.HTTP:URI-HTTPS https://simple-tool-rental-api.glitch.me/api-clients>
    #<CL+SSL::SSL-STREAM for #<FD-STREAM for "socket 192.168.1.53:39148, peer: 18.205.205.44:443" {10035F4C33}>>
    

    I use the "pete" email because the pedro one is "already registered". I use hand-formatted JSON to send the credentials with a Content-Type of application/json, which format I am sure about. I wanted to use a "raw" content-type as in the Postman screen capture, but wasn't sure the alist played nice with it.

    I think by knowing better the example API you could sort out another content-type to use with the alist body content.