DEV Community

Cover image for Testing your API with REST Client

Testing your API with REST Client

Alexandre Nédélec on March 05, 2019

Let's talk about tooling and testing an API ! GUI Tools and their limits Like most developers I guess, I often use GUI tools like Fiddl...
Collapse
 
icepickle profile image
Brecht Pynoo

Although I liked the tool for easily writing requests against the server, I should disagree with the fact that it does testing, you just perform a request and get a response from the server.

Is there any way to actually do non-manual testing with it, short of generating the code snippet for it and pasting it inside a test?

Collapse
 
techwatching profile image
Alexandre Nédélec

Yes you are right. I was not talking about automated testing or something like that. I was only talking to manually test your API. I use it mainly for hitting a breakpoint in my API and debug my code.

I am not aware of a way of doing automated testing with that but if you found something I would be glad to hear about it.

Collapse
 
mav1283 profile image
Paolo

How do you add a parameter in the url?

Collapse
 
techwatching profile image
Alexandre Nédélec

Do you mean like that ? Or something else ?

### Search planet with a specific name 
@planetName = Naboo
GET https://swapi.co/api/planets/?search={{planetName}} HTTP/1.1
Collapse
 
mav1283 profile image
Paolo

I mean if you're working on a crud operations and you need to pass a parameter

Thread Thread
 
techwatching profile image
Alexandre Nédélec

In my example you can just change the value of the planetName variable. You can also do not use local variable and set environment variable in your vs code settings (rest-client.environmentVariables)

Thread Thread
 
noble47 profile image
Noble-47

Just came across your article and it was really helpful. But I still don't understand how to pass parameters in POST request

Thread Thread
 
techwatching profile image
Alexandre Nédélec

I have written an example in a response to a comment above.
You can do something like the following. Here I am defining a variable @test with a value of HELLO and using this parameter in my POST request.
@test is your parameter, you just have to change its value to pass a different value in your request.

### Second request using a variable 
@test = HELLO
POST https://jsonplaceholder.typicode.com/posts
Content-Type: application/json

{
    "userId": 1,
    "title": "TEST ",
    "body": "TEST body {{test}}"
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
nirmal_kumar profile image
Nirmal

I have been using REST Client for a long time and never knew that we can use variables and we can also pass values from previous results. Thanks for sharing.

Collapse
 
developerhakart profile image
developerhakart

As I understood this is only for VS Code what about alternative in Visual Studio ?

Collapse
 
techwatching profile image
Alexandre Nédélec

I don't know an alternative for Visual Studio but I would say you don't really need to. You can do your usual development in Visual Studio and have VS code opened to do other things like querying your API.

Collapse
 
techwatching profile image
Alexandre Nédélec

There is one alternative for Visual Studio now called Rest Client (for Visual Studio). It's an extension as well

Collapse
 
chiangs profile image
Stephen Chiang • Edited

Insomnia also allows for assigning variables for each environment, really helps with having to update a whole set of requests or when an auth token is required.

The one thing postman does that is nice is the auto generation of documentation, but if you're using .net back end you can do that too with swagger UI.

Collapse
 
timothytu profile image
Timothy

Can the variables be used inside POST data? I have added an item in a request. I can get the id from the response. Then I want to use the id in a different POST request. It doesn't seem to work:-(

Collapse
 
techwatching profile image
Alexandre Nédélec

Yes you can, you just have to call the first request, then the second request.
You can find below an example. I tested it, it works fine.

### First request
# @name myRequest
POST https://jsonplaceholder.typicode.com/posts
Content-Type: application/json

{
    "userId": 1,
    "title": "TEST 18",
    "body": "TEST body 18"
}

### Second request using response in body 
POST https://jsonplaceholder.typicode.com/posts
Content-Type: application/json

{
    "userId": 1,
    "title": "TEST ",
    "body": "TEST body {{myRequest.response.body.id}}"
}

### Second request using a variable 
@test2 = {{myRequest.response.body.id}}
POST https://jsonplaceholder.typicode.com/posts
Content-Type: application/json

{
    "userId": 1,
    "title": "TEST ",
    "body": "TEST body {{test2}}"
}
Collapse
 
devopsrey profile image
devopsRey

1) Is there anyway I can hit the multiple endpoints
2) Is there anyway to put some assertions

Collapse
 
techwatching profile image
Alexandre Nédélec

1) What do you mean by multiple endpoints ? You can use variables to change the environment / url you are targetting.
2) I don't know about that but I don't think so.