🪟 Windows TippsThe Gemini desktop app is now available for Windows(11.09.2026 um 17:06 Uhr)
⚠️ Malware / Trojaner / VirenWindows 11 just dropped the tool ransomware abused, Microsoft says don’t restore WMIC(10.09.2026 um 20:11 Uhr)
⚠️ Malware / Trojaner / VirenVorsicht: Android-Malware verschlüsselt Ihre Handys und nimmt heimlich Fotos auf(11.09.2026 um 09:35 Uhr)
🕵️ SicherheitslückenMicrosoft geht endlich eines der nervigsten Probleme von Windows 11 an(11.09.2026 um 11:58 Uhr)
💾 IT Security ToolsSysinternals Suite(11.09.2026 um 12:00 Uhr)
🕵️ SicherheitslückenDefender 0-Day ShieldBreak (CVE-2026-69414) nicht sauber gepatcht - BornCity(11.09.2026 um 12:52 Uhr)
🪟 Windows TippsThe Gemini desktop app is now available for Windows(11.09.2026 um 17:06 Uhr)
⚠️ Malware / Trojaner / VirenWindows 11 just dropped the tool ransomware abused, Microsoft says don’t restore WMIC(10.09.2026 um 20:11 Uhr)
⚠️ Malware / Trojaner / VirenVorsicht: Android-Malware verschlüsselt Ihre Handys und nimmt heimlich Fotos auf(11.09.2026 um 09:35 Uhr)
🕵️ SicherheitslückenMicrosoft geht endlich eines der nervigsten Probleme von Windows 11 an(11.09.2026 um 11:58 Uhr)
💾 IT Security ToolsSysinternals Suite(11.09.2026 um 12:00 Uhr)
🕵️ SicherheitslückenDefender 0-Day ShieldBreak (CVE-2026-69414) nicht sauber gepatcht - BornCity(11.09.2026 um 12:52 Uhr)

🔧 Programmierung 🕛 vor 6 Monaten 4 Min Lesezeit
0

10 Practical Script Examples for API Testing

↗ Quelle (dev.to)
🗣️ Stimme:
📑 Inhaltsübersicht

API testing gets serious the moment you stop clicking buttons and start scripting real-world scenarios. If you’re working with any API client that supports pre request and post response scripts, you can automate validations, chain requests, mock edge cases, and catch bugs before they hit production.



Here are 10 practical script examples you can actually use in day-to-day API testing






1. Validate Status Code and Response Time



Always validate that your API responds correctly and within acceptable time.




CODE

rq.test("Status code is 200", () => {
rq.expect(rq.response.code).to.equal(200);
});

rq.test("Response time is under 500ms", () => {
rq.expect(rq.response.responseTime).to.be.lessThan(500);
});







This is essential for login endpoints, health checks, and critical APIs.






2. Validate Required Fields in Response



Instead of manually checking fields, automate it.




CODE

const data = rq.response.json();

rq.test("Response contains required fields", () => {
rq.expect(data).to.have.property("id");
rq.expect(data).to.have.property("email");
rq.expect(data).to.have.property("name");
});







This protects you from accidental backend changes.






3. Extract Auth Token and Store It



Most APIs require authentication. Store the token for later requests.




CODE

const body = rq.response.json();

rq.environment.set("authToken", body.token);

rq.test("Token saved successfully", () => {
rq.expect(rq.environment.get("authToken")).to.be.ok;
});








Then use {{authToken}} in your Authorization header for subsequent requests.






4. Ensure Fields Are Not Empty



Sometimes fields exist but contain empty values.




CODE

const data = rq.response.json();

rq.test("Email is not empty", () => {
rq.expect(data.email).to.be.a("string").and.not.be.empty;
});

rq.test("Name is not empty", () => {
rq.expect(data.name).to.be.a("string").and.not.be.empty;
});







Useful for profile and checkout validations.






5. Validate Array Length



If an endpoint returns a list, confirm it contains data.




CODE

const list = rq.response.json();

rq.test("At least one item returned", () => {
rq.expect(list.length).to.be.greaterThan(0);
});







Important for search and listing APIs.






6. Conditional Testing Based on Response Data



Sometimes validation depends on the role or type returned.




CODE

const data = rq.response.json();

if (data.role === "admin") {
rq.test("Admin has permissions array", () => {
rq.expect(data.permissions).to.be.an("array");
});
}








This helps validate role based access logic.






7. Negative Testing for Invalid Input



Do not test only successful cases. Break the API intentionally.




CODE

rq.test("Returns 400 for invalid input", () => {
rq.expect(rq.response.code).to.equal(400);
});

const error = rq.response.json();

rq.test("Correct error message returned", () => {
rq.expect(error.message).to.equal("Invalid input provided");
});








This ensures error handling works as expected.






8. Chain Requests Using Dynamic IDs



Create a resource and reuse its ID in the next request.




CODE

const data = rq.response.json();

rq.environment.set("userId", data.id);

rq.test("User ID stored", () => {
rq.expect(rq.environment.get("userId")).to.be.ok;
});








Now use {{userId}} in the next request URL.



This simulates real workflows instead of isolated API calls.






9. Validate Business Logic Calculations



Go beyond structure validation and test logic.




CODE

const body = rq.response.json();
let calculatedTotal = 0;

body.items.forEach(item => {
calculatedTotal += item.price * item.quantity;
});

rq.test("Total amount is correct", () => {
rq.expect(body.totalAmount).to.equal(calculatedTotal);
});








This catches pricing and calculation bugs early.






10. Store and Reuse Custom Variables



You can also store custom values for later use.




CODE

rq.variables.set("currentUserEmail", rq.response.json().email);

rq.test("Email variable stored", () => {
rq.expect(rq.variables.get("currentUserEmail")).to.be.ok;
});







This is helpful for multi-step API flows.






Why This Matters



Checking only status codes is shallow testing. Real API testing means validating structure, performance, data correctness, error responses, and business logic.



Requestly’s scripting capability lets you:




  • Automate response validation

  • Chain multi-step workflows

  • Test negative scenarios

  • Validate complex business rules

  • Reuse dynamic values



Start with a few meaningful assertions per endpoint. Expand into workflow validation once the basics are solid.



API testing is not about sending requests. It is about enforcing contracts and preventing production issues.

Vollständiger Original-Bericht
Ausführliche Details, Code-Beispiele & Hersteller-Stellungnahme auf dev.to.
↗ Original-Artikel auf dev.to lesen
Wie bewertest du diesen Beitrag?
1 Klick Feedback
Teilen mit Netzwerk & Team:

Community-Analysen & Experten-Meinungen 0

Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
Community Pulse: Relevanz-Einschätzung
1 Klick Experten-Votum
🔴 Akute Relevanz 0%
🟡 In Evaluierung 0%
🟢 Keine Auswirkung 0%
Spannende Innovation 0%
Verwandte Story-Cluster & Quellen (Vektor-KI)
Port 8095 Engine
1 Quelle
The Gemini desktop app is now available for Windows
1 Quelle
Windows 11 just dropped the tool ransomware abused, Microsoft says don’t restore WMIC
1 Quelle
Vorsicht: Android-Malware verschlüsselt Ihre Handys und nimmt heimlich Fotos auf
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten 10 Practical Script Examples for API Testing

Thematisch verwandte Begriffe: Practical, Script, Examples, Testing · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...