DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • The Next Evolution of Java: Faster Innovation, Simpler Adoption
  • API Testing With Cypress
  • Mutual TLS With gRPC Between Python and Go Services
  • How to Do API Testing?

Trending

  • The Role of Functional Programming in Modern Software Development
  • It’s Not About Control — It’s About Collaboration Between Architecture and Security
  • Debugging Core Dump Files on Linux - A Detailed Guide
  • SQL Server Index Optimization Strategies: Best Practices with Ola Hallengren’s Scripts
  1. DZone
  2. Software Design and Architecture
  3. Integration
  4. API Testing Using Python Script

API Testing Using Python Script

Are you trying to test API in your project without a tool? Here comes the Requests library, one of the powerful and standard libraries in Python.

By 
Srilakshmi Srinadhuni user avatar
Srilakshmi Srinadhuni
·
Nov. 23, 20 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
27.1K Views

Join the DZone community and get the full member experience.

Join For Free

Are you trying to test API in your project without a tool? Here comes the Requests library, one of the powerful and standard libraries in Python.

Overview:

In this article, we will understand how to do API testing using simple python scripts. We shall make use of the Requests library of python to handle HTTP requests. The simple import of this library and any IDE ( eclipse,pycharm, IntelliJ) would suffice, and there is no need for any specific tool to accomplish API testing.

We shall cover the following topics in this article:

  • Install Requests library
  • Implement HTTP GET request
  • Implement HTTP POST request
  • Implement HTTP PUT request
  • Implement HTTP DELETE request

Install Requests in Python:

Run the Command “python -m pip install requests” in the Command Prompt.

run the command

Requests library comes with different features, which will make testing of HTTP requests much easy and flexible. We can implement all the HTTP methods, extract content from headers, verify authorization, and customize the requests by sending different query parameters and body messages.

We shall use the Eclipse IDE ( Pydev plugin added) to execute different HTTP requests.

For this demo, I have installed a small application on NodeJS, which has few API requests in JSON format. In this sample application, I have added 3 resources Tutorial, subscriptions, and comments. This sample API can take tutorial id and provide the tutorial details as a response

tutorialName

Let us discuss how we can implement different HTTP methods on this sample application using python.

1. Implement HTTP GET Request:

Let us execute the most used HTTP method, “GET,” which retrieves the requested resource data. We can also send query parameters to retrieve specific data.

import JSON

Console: Here is the response after invoking the requested URL.

requested URL

  • We can also modify the GET request by adding query parameters. Here in the same example, if we would want to retrieve tutorial details for 1006, then provide as below:

parms

  • I have created a variable response_code in which the results are captured. Here response_code carries the return status code, i.e., 200 in this example.
  • In case we want the response details, we can use the .json() method whose return type is a dictionary ( key-value pairs).

Similarly, we can fetch the response header details using .headers and .cookiesprint(response)

2. Implement HTTP POST Request:

To execute the POST method, there is a need to send data through the message body. The data can be sent using the data parameter within the Post method.

In the below example, we have added data parameters in the post method.HTTP post requestIf we see the response, the return code is 201 (which implies a record is created), and the record is added.

3. Implement HTTP PUT Request:

PUT request behaves in two different ways. If the requested resource is already existing, it updates the details; it creates the new record. We can pass the data as a body parameter.PUT request

4. Implement HTTP DELETE Request:

Delete request deletes the specified resource identified in the request URI. We can send that specific id either as part of URI or by using param(parameters).HTTP delete request

Conclusion:

We have now explored and implemented different methods within the API. These are few features of the Requests library in python.

API Python (language) API testing Requests

Opinions expressed by DZone contributors are their own.

Related

  • The Next Evolution of Java: Faster Innovation, Simpler Adoption
  • API Testing With Cypress
  • Mutual TLS With gRPC Between Python and Go Services
  • How to Do API Testing?

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!