Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: curl --write-out support #50

Open
kareem-abdul opened this issue Sep 11, 2024 · 2 comments
Open

Feature: curl --write-out support #50

kareem-abdul opened this issue Sep 11, 2024 · 2 comments

Comments

@kareem-abdul
Copy link

kareem-abdul commented Sep 11, 2024

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
Copy link
Author

kareem-abdul commented Sep 11, 2024

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
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants