Similarly to the command line tools, it is possible to define requests using a concise text syntax, storing requests in text files for easy modification and copying and pasting requests and responses, but this time all inside a code editor. I am mainly a VS Code user, so I will show examples using the former. The main ones are REST Client for Visual Studio Code and the native IntelliJ HTTP Client.īoth are excellent alternatives to previous approaches. In software development, scratchpads typically involve writing and executing code, but there are some for sending HTTP requests as well. Scratchpads are typically single-file environments to experiment and play. But even for simple API invocations, the abilities are pretty good! Scratchpad-like tools It is fair to say that besides ad-hoc API usage and testing, traditional GUI clients often contain other features like request history, sharing predefined requests across a team, running automated test suites, or tools for designing new APIs. Accept curl commands: Going back from a curl request to Insomnia is also possible! To do so, copy and paste your curl command into the URL text field, and Insomnia will parse it.Learning how to make curl requests is also helpful, as we can create them in Insomnia and convert them later. Copy as curl: The endpoint context menu offers an option to copy a request as curl, which can be useful when sending the request to another person quickly or continuing the invocation from the command line.Insomnia has two neat features that can transform the testing in and out of the command line: Clicking on "Manage environments" from the top-left context menu for environments or pressing CTRL + E will open a configuration like this:Īs we discussed, we can utilize curl commands in our API testing on the command line. One cool thing about Insomnia is that we can manage multiple environment configurations and define custom environments that can be reused everywhere, e.g., in the URL definitions, in request bodies, or headers. When the API spec is unavailable yet or cannot be imported (e.g., due to an error in the spec), requests can still be crafted by hand in a visual interface.Ĭompared to other approaches, these clients also offer built-in organization of endpoint calls into collections and request chaining, as we will see in the following examples demonstrating the use of Insomnia. The significant advantage over other tools is that we can typically use an existing API specification, like OpenAPI, to load a collection of API endpoints and use them immediately with only minor changes. Traditional GUI HTTP clients like Postman or Insomnia are usually a good start for using and testing APIs. New to the Linux command line? You can learn the fundamentals and all the cool stuff in my book Command Line: A Modern Introduction. However, it can be helpful to end up with a script for future reuse. JWT = $(http POST :8000/api/user/token-auth/ username = $USER password =$PASSWD -print b | jq '.token' -raw-output )Ĭombining results like we just saw is a bit more complex. If we take all the commands together and put them in a file, we can even create a simple shell script that could be run repeatedly: #!/usr/bin/bash We can use the variable in the next endpoint call ( $JWT). Finally, the special syntax $( ) will ensure that the whole command inside it is run first, and its result is passed to the top-level command that assigns it to a shell variable. raw-output will discard the quotation marks. First, I told HTTPie to output only the response body without the headers ( -print b), pass the output to the JSON command-line parsing tool jq using the pipe ( |), and extract the top-level token property in the JSON file. The above example assumes that the token is returned in the response JSON as a top-level property. > JWT = $(http POST :8000/api/user/token-auth/ username = $USER password =$PASSWD -print b | jq '.token' -raw-output ) Notice that in HTTPie it is enough to define just the port :8000 if the target is localhost, set a different HTTP method ( POST) as a second argument, and describe the top-level JSON body by separate key=value arguments: > USER PASSWD =test Let's look at another example, where I am passing a username and password to an authentication endpoint to get a JSON Web Token and use that token in the subsequent request. But how to take data from one request to another? The secret to request chaining is to parse the response and store it in a shell variable. All we have to do is to issue more commands. Sending multiple requests is trivial in shell.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |