<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Run Basic API Request &#8211; TestAutomationLabs</title>
	<atom:link href="https://testautomationlabs.com/tag/run-basic-api-request/feed/" rel="self" type="application/rss+xml" />
	<link>https://testautomationlabs.com</link>
	<description>Quality Matters</description>
	<lastBuildDate>Sat, 22 Feb 2025 15:18:28 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://testautomationlabs.com/wp-content/uploads/2023/08/imageedit_2_5514276710-150x150.png</url>
	<title>Run Basic API Request &#8211; TestAutomationLabs</title>
	<link>https://testautomationlabs.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>How to Run Basic API Request Using Ruby and Watir</title>
		<link>https://testautomationlabs.com/run-basic-api-request-using-watir/</link>
					<comments>https://testautomationlabs.com/run-basic-api-request-using-watir/#respond</comments>
		
		<dc:creator><![CDATA[TestAutomationLabs]]></dc:creator>
		<pubDate>Thu, 17 Aug 2023 13:34:58 +0000</pubDate>
				<category><![CDATA[Automation Testing]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Run Basic API Request]]></category>
		<guid isPermaLink="false">https://testautomationlabs.com/?p=312</guid>

					<description><![CDATA[Watir know as Web Application Testing tool in Ruby used for automating tests for web applications. If you want to run basic API request using Ruby or Watir, you will need to use an additional libraries such as HTTParty or RestClient, since Watir is primarily designed for web browser automation. REST API in Ruby is [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p>Watir know as Web Application Testing tool in Ruby used for automating tests for web applications. If you want to run basic API request using Ruby or Watir, you will need to use an additional libraries such as HTTParty or RestClient, since Watir is primarily designed for web browser automation. REST API in Ruby is not so complex we can easily learn this in few minutes, so now lets start our main course.</p>



<h2 class="wp-block-heading">Here&#8217;s a general guideline on how to run basic API request using Ruby language</h2>



<p>To Run Basic API Request Using Watir / Ruby, Start by installing the necessary gems for both Watir and required other libraries. You can also use <code>bundler</code> to manage your gem dependencies. </p>



<h3 class="wp-block-heading">Installing Required Ruby Gems</h3>



<p>First Install following gems using Terminal or command line</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: bash; title: ; notranslate">
gem install watir
gem install httparty   # or gem install rest-client
</pre></div>


<h3 class="wp-block-heading">Import Libraries</h3>



<p>Before Writing any script, you need to import require libraries you&#8217;ll be using</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: ruby; title: ; notranslate">
require &#039;watir&#039;
require &#039;httparty&#039;    # or require &#039;rest-client&#039;
</pre></div>


<h3 class="wp-block-heading">Initialise Watir</h3>



<p>Setup Watir to interact with browser.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: ruby; title: ; notranslate">
browser = Watir::Browser.new(:chrome)  # Or :firefox, :safari, etc.
</pre></div>


<h3 class="wp-block-heading">Make API Calls</h3>



<p>You can use chosen library (RestClient or HTTParty) to make API calls. Let&#8217;s see simple example to understand it.</p>



<h4 class="wp-block-heading">Call API with HTTParty</h4>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: ruby; title: ; notranslate">
response = HTTParty.get(&#039;https://api.example.com/testdata&#039;)
puts response.code      # HTTP status code
puts response.body      # Response body
</pre></div>


<h4 class="wp-block-heading">And here&#8217;s an example of RestClient</h4>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: ruby; title: ; notranslate">
response = RestClient.get(&#039;https://api.example.com/testdata&#039;)
puts response.code      # HTTP status code
puts response.body      # Response body
</pre></div>


<h3 class="wp-block-heading">Clean Up</h3>



<p>After making API calls successfully, don&#8217;t forgot to close the browser if you no longer need it.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: ruby; title: ; notranslate">
browser.close
</pre></div>


<p>Now lets see some proper example to manage all these professionally in OOP approach (<strong>POST Request using Ruby</strong>)</p>



<p>Create common utils for post request (You can create separate ruby file for this) : Here you can modify post body as per your requirements. This method will prevent you from writing same code multiple times.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: ruby; title: ; notranslate">
def post_request(url, query_param = {}, headers = {}, body = {}, redirects = true)
	log(&quot;API Request URL: #{url}&quot;)
	response = HTTParty.post(url, query: query_param, headers: headers, body: body, follow_redirects: redirects)
	log(&quot;Response: #{response}&quot;)
	response
end
</pre></div>


<p>Create separate method in your test script file to manage your code. This will help you to write clean and manageable code.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: ruby; title: ; notranslate">
def post_create_user(query_param = {})
  api_url = &quot;Your API Here&quot;
  response = post_request(api_url, query_param, nil, nil)
  response
end
</pre></div>


<p>Now Create test method to utilise above logics and perform some assertions.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: ruby; title: ; notranslate">
def check_create_user
	test_data = 	{
		table_name: &#039;users&#039;,
		user_name: &#039;testautomationlabs&#039;,
		password: &#039;iloveautomation&#039;,
		user_role: &#039;admin&#039;,
		user_bio: &#039;Hello World, This is my awesome bio&#039;
	}
	response = post_create_user(test_data)
	# Assert the status code
	expected_status_code = 200
	if response.code == expected_status_code
		puts &quot;API call successful: Status code is #{expected_status_code}&quot;
	else
		puts &quot;API call failed: Expected status code #{expected_status_code}, but got #{response.code}&quot;
	end
end
</pre></div>]]></content:encoded>
					
					<wfw:commentRss>https://testautomationlabs.com/run-basic-api-request-using-watir/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
