oysandvik94 / curl.nvim

Run requests with curl, formatted with jq, and persisted commands according to your own workflow
113 stars 3 forks source link

Feature: curl --write-out support #50

Open kareem-abdul opened 2 months ago

kareem-abdul commented 2 months ago

Hi, Curls man page defines an option called --write-out which you can use to output in custom formats. Ussually, I use this feature to get the request/response times, debug ssl, redirects etc. The formats can be specified either inline in the command like this curl -w "%{total_time}" https://example.com or in a different file like following

time_namelookup: %{time_namelookup}\n
time_connect: %{time_connect}\n
time_appconnect: %{time_appconnect}\n
time_pretransfer: %{time_pretransfer}\n
time_redirect: %{time_redirect}\n
time_starttransfer: %{time_starttransfer}\n
———\n
time_total: %{time_total}\n

and use it in the command like so curl -w "@curl-filename.txt"

Right now, it is somewhat possible to do this, as it is just a flag for curl. But the output looses its format. I assume that this is because of jq eg: you could do this request

curl -XGET https://jsonplaceholder.typicode.com/todos
-H "Content-Type: application/json"
-w "totalTime: %{time_total}"

This will give the results in a single line, with the custom format at the end of it image

I think it would be great if the plugin had explicit support for this. That is, users can define a format file (or an inline format) in the config, or the plugin detects the -w flag in the curl file, and outputs the formatted result ,maybe in the top of the output buffer, just like how headers are shown for the -i flag.

kareem-abdul commented 2 months ago

I was searching if it is possible to have a wrapper around this custom output, so that the plugin can maybe detect it in some way (maybe a treesitter token?). And it seems the following works

curl -s -w "\n<custom>%{time_total}\n%{time_redirect}</custom>"  -XGET https://jsonplaceholder.typicode.com/todos

which results in the following output

[                                                                                                                                                                                                                                           
  {                                                                                                                                                                                                                                         
    "userId": 1,                                                                                                                                                                                                                            
    "id": 1,                                                                                                                                                                                                                                
    "title": "delectus aut autem",                                                                                                                                                                                                          
    "completed": false                                                                                                                                                                                                                      
  },                                                                                                                                                                                                                                        
  {                                                                                                                                                                                                                                         
    "userId": 1,                                                                                                                                                                                                                            
    "id": 2,                                                                                                                                                                                                                                
    "title": "quis ut nam facilis et officia qui",                                                                                                                                                                                          
    "completed": false                                                                                                                                                                                                                      
  }
]
<custom>1.147859
0.000000</custom>
oysandvik94 commented 2 months ago

Great idea, I did not know about this feature! I'll play around with it a little and see if I can come up with a nice solution.