Skip to content

Commit 1a6bfd1

Browse files
committed
added cli solution description
1 parent c3807a6 commit 1a6bfd1

File tree

1 file changed

+107
-0
lines changed

1 file changed

+107
-0
lines changed

challenger/src/main/resources/content/apichallenges/solutions/miscellaneous/delete-all-todos.md

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,110 @@ Some tools have the ability to issue Data Driven requests, so if you can parse t
4444

4545
Most of the API client tools also have the ability to create scripts to achieve this.
4646

47+
## Deleting All `todo`s using Command Line Tools
48+
49+
I'm going use standard Linux/Unix/Bash commands here.
50+
51+
I can use a combination of `curl`, `jq`, and `xargs` to achieve what I want.
52+
53+
- [cUrl](https://curl.se/) command line HTTP client
54+
- [jq](https://jqlang.github.io/jq/) a command line JSON processor
55+
- [xargs](https://pubs.opengroup.org/onlinepubs/9799919799/utilities/xargs.html) piping parameters into commands [examples](https://en.wikipedia.org/wiki/Xargs)
56+
57+
I can use all these linux commands on Windows using a WSL ((Windows Subsystem for Linux)[https://learn.microsoft.com/en-us/windows/wsl/about]) which means I can use the same commands as a mac and make this solution portable.
58+
59+
I'm running Ubuntu in my WSL
60+
61+
First issue a curl request to get the todos in API challenges.
62+
63+
If I don't know how to create the `culr` command I can generate the `curl` command easily by using Bruno to generate the code (most REST Clients also have this feature).
64+
65+
- create request in Bruno with the X-CHALLENGER header
66+
- try it out
67+
- right click on the item in the left side bar and `Generate Code` and then choose `Shell-Curl`
68+
69+
```
70+
curl --request GET \
71+
--url https://apichallenges.eviltester.com/todos \
72+
--header 'X-CHALLENGER: 07466215-9bab-4bf4-9b7d-34b7ac765915'
73+
```
74+
75+
Replace the `X-CHALLENGER` GUID with your GUID
76+
77+
I will actually save the response into a file to make it easier to work with.
78+
79+
```
80+
curl --request GET \
81+
--url https://apichallenges.eviltester.com/todos \
82+
--header 'X-CHALLENGER: 07466215-9bab-4bf4-9b7d-34b7ac765915'
83+
> todos.json
84+
```
85+
86+
The response is JSON, I want to parse that JSON to find all the ids, because I'm going to use that list of ids to delete them all.
87+
88+
I will use JQ for that.
89+
90+
The command below iterates over all the items in the todo array and extracts the id
91+
92+
```
93+
jq '.todos[].id' todos.json
94+
```
95+
96+
I can save this list to a file
97+
98+
```
99+
jq '.todos[].id' todos.json > ids.txt
100+
```
101+
102+
Then if I `cat ids.txt` I can see all the ids
103+
104+
```
105+
> cat ids.txt
106+
1
107+
8
108+
5
109+
2
110+
6
111+
3
112+
9
113+
7
114+
4
115+
10
116+
```
117+
118+
I then want to call curl for each of these values and I can use `xargs` to do that
119+
120+
```
121+
cat ids.txt | xargs -I % curl --request DELETE \
122+
--url https://apichallenges.eviltester.com/todos/% \
123+
--header 'X-CHALLENGER: 07466215-9bab-4bf4-9b7d-34b7ac765915'
124+
```
125+
126+
I can double check that I have deleted them by looking at the API Challenges progress page.
127+
128+
Or just call the `GET /todos` and see an empty array
129+
130+
```
131+
> curl --request GET --url https://apichallenges.eviltester.com/todos --header 'X-CHALLENGER: 07466215-9bab-4bf4-9b7d-34b7ac765915'
132+
133+
{"todos":[]}
134+
```
135+
136+
137+
Or I could do it all in one command:
138+
139+
```
140+
curl --request GET \
141+
--url https://apichallenges.eviltester.com/todos \
142+
--header 'X-CHALLENGER: 880c5857-dbff-4266-b419-701efa804679' |
143+
jq '.todos[].id' |
144+
xargs -I % curl --request DELETE \
145+
--url https://apichallenges.eviltester.com/todos/% \
146+
--header 'X-CHALLENGER: 880c5857-dbff-4266-b419-701efa804679'
147+
```
148+
149+
## Delete All Todos Using Command Line Tools Video
150+
151+
{{<youtube-embed key="7Kz97rn7f3I" title="Solution to Delete all Todos in default format">}}
152+
153+
[Patreon ad free version](https://www.patreon.com/posts/119362209)

0 commit comments

Comments
 (0)