CURL响应处理:如何手动打开请求返回JSON中的链接
Hey there! Let's cut out the manual copy-paste grind and get those links opened smoothly. Here are some practical, actionable ways to tackle this:
Extract links cleanly with
jq(Linux/macOS)
If you havejqinstalled (grab it viabrew install jqon macOS orsudo apt install jqon Debian/Ubuntu), you can parse the JSON response directly to pull out all links. For example, if your links live in alinksarray under thedatakey:curl https://your-api-endpoint.com | jq -r '.data.links[]'The
-rflag outputs raw strings (no extra quotes around links), making them ready to use right away.Automatically open all links in one go
Once you can extract the links, pipe them to your system's default browser command to skip manual opening:- On macOS:
curl https://your-api-endpoint.com | jq -r '.data.links[]' | xargs open - On Linux:
curl https://your-api-endpoint.com | jq -r '.data.links[]' | xargs xdg-open
This will launch each link in your default browser automatically.
- On macOS:
Windows PowerShell alternative
If you're on Windows, skip third-party tools and use PowerShell's built-in JSON parsing:# Fetch the JSON response $apiResponse = Invoke-RestMethod -Uri "https://your-api-endpoint.com" # Loop through each link and open it foreach ($link in $apiResponse.data.links) { Start-Process $link }This will open every link in your default Windows browser without extra setup.
Manual fallback (no tools needed)
If you don't want to install anything, copy the full JSON output from curl into a text editor with JSON formatting support (like VS Code with its built-in JSON extension). Format the JSON (usuallyCtrl+Shift+Iin VS Code), then locate the links field and copy-paste each link into your browser.
Just tweak the JSON path (.data.links[] in the examples) to match the actual structure of your API's response, and you're set!
内容的提问来源于stack exchange,提问作者Graj

