<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Bilal Sheikh Khalil on Medium]]></title>
        <description><![CDATA[Stories by Bilal Sheikh Khalil on Medium]]></description>
        <link>https://medium.com/@bilalsheikhkhalil?source=rss-392fd72485e5------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*jJosUWTd-7DuaQvVlPyIPA.png</url>
            <title>Stories by Bilal Sheikh Khalil on Medium</title>
            <link>https://medium.com/@bilalsheikhkhalil?source=rss-392fd72485e5------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 03:29:50 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@bilalsheikhkhalil/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How to make HTTP requests by using the Axios library in React Native Apps.]]></title>
            <link>https://bilalsheikhkhalil.medium.com/how-to-make-http-requests-by-using-the-axios-library-in-react-native-apps-4e506af9b80f?source=rss-392fd72485e5------2</link>
            <guid isPermaLink="false">https://medium.com/p/4e506af9b80f</guid>
            <category><![CDATA[restful-api]]></category>
            <category><![CDATA[axios]]></category>
            <category><![CDATA[react-native]]></category>
            <category><![CDATA[fetch-api]]></category>
            <dc:creator><![CDATA[Bilal Sheikh Khalil]]></dc:creator>
            <pubDate>Sun, 27 Dec 2020 15:55:34 GMT</pubDate>
            <atom:updated>2020-12-27T17:26:25.942Z</atom:updated>
            <content:encoded><![CDATA[<h3><strong>How to make an HTTPS request by Axios library in React Native.</strong></h3><p>The internet boasts a vast array of resources hosted on different servers. For you to access these resources, let’s say your browser or mobile device needs to be able to send a request to the servers and display the resources for you. HTTPS (Hypertext Transfer Protocol), is the underlying format that is used to structure requests and responses for effective communication between a client and a server. The message that is sent by a client to a server is what is known as an HTTPS request. When these requests are being sent, clients can use various methods like POST, GET, DELETE, etc...</p><h4>How Do HTTPS Requests Work?</h4><p>HTTPS requests work as the intermediary transportation method between a client/application and a server. The client submits an HTTPS request to the server, and after internalizing the message, the server sends back a response. The response contains status information about the request like the photo below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SJ0Q1_d5zCyo5TS3s7NLkw.png" /><figcaption>HTTPS request</figcaption></figure><p>In this article, we gonna use The <a href="https://newsapi.org/">News API</a> to get list of latest news.<br>first of all, you have to register on that website to get an api_key so without this key you’ll not be able to make a request.</p><h4>Create and Running your React Native application:</h4><h4>Step 1: Create a React Native app</h4><pre>$ npx react-native init your_project_name</pre><p>If we use Yarn as a package manager we will add the following lines:</p><pre>$ npm install -g yarn<br>$ yarn global add react-native-cli<br>$ react-native init sample</pre><h4>Step2: add the <a href="https://github.com/axios/axios">Axios</a> library to the project:</h4><p>first, we have to change the directory to project path in our device:</p><pre>$ cd your_project_path</pre><p>after that for the users who are using the NPM package manager should add the following line in the terminal:</p><pre>$ npm install axios</pre><p>for <em>Yarn</em> package manager users should add the line:</p><pre>$ yarn add axios</pre><h4>Step3: Running the project</h4><p>Using <em>NPM</em> as a package manager:</p><pre>// IOS Platform<br>$ npx react-native run-ios<br>// Android Platform<br>$ npx react-native run-android</pre><p>Using <em>Yarn</em> as a package manager:</p><pre>// IOS Platform<br>$ yarn ios<br>// Android Platform<br>$ yarn android</pre><p>we will see the initial following screen:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/828/1*G6Inrhbz9xncozuTkF4gWw.png" /></figure><p>Then we have to create a custom component and name it NewsCard.js</p><p>it contains the title, author, image, and description of the news and we will give him some StyleSheet properties like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*t_hfu0BiarLxydpGZjeJBg.png" /><figcaption>NewsCard.js</figcaption></figure><h4>Making Axios get request to fetch a list of news:</h4><p>In order to fetch News from a URL, we can pass the URL to fetch:</p><pre>axios.get(baseURL);</pre><p>Get method also takes an optional second argument that allows us to customize the HTTPS request. We may want to specify params as a JSON object when there are query parameters in the URL like our example:</p><pre>axios.get(baseURL, {<br>  params: {<br>  country: &#39;us&#39;,<br>  apiKey: &#39;aa6a097fb9fb4509958fdabd1942e6d1&#39;,<br>  },<br>})</pre><p>Networking is an asynchronous operation. Get method will return a <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise">Promise</a> that makes it straightforward to write code that works in an asynchronous manner:</p><pre>axios.get(baseURL, {<br> params: {<br> country: &#39;us&#39;,<br> apiKey: &#39;aa6a097fb9fb4509958fdabd1942e6d1&#39;,<br> },<br>})<br> .then(async function (response) {<br> //do anything you want in our example we set the news <br> setNews(response.data);<br>})</pre><p>The last thing we have to take into consideration is catching the error if the response has failed by using the catch() method like this:</p><pre>axios.get(baseURL, {<br> params: {<br> country: &#39;us&#39;,<br> apiKey: &#39;aa6a097fb9fb4509958fdabd1942e6d1&#39;,<br> },<br>})<br> .then(async function (response) {<br> //do anything you want in our example we set the news <br> setNews(response.data);<br>})<br>.catch((error) =&gt; {<br> //do whatever you want let&#39;s say print the error in the console.<br> console.log(error)<br>});</pre><p>After we have finished the networking operations we would create the main screen which name is News.js, it will contain FlatList inside this component we will render the news items data as useState that have just received from the response like this:</p><pre>const renderNewsList = ({item}) =&gt; &lt;NewsCard item={item} /&gt;;</pre><pre>return (<br> &lt;SafeAreaView&gt;<br>  &lt;View&gt;<br>   &lt;FlatList<br>      data={news.articles}<br>      keyExtractor={(item, index) =&gt; &#39;key&#39; + index}<br>      renderItem={renderNewsList}<br>   /&gt;<br> &lt;/View&gt;<br>&lt;/SafeAreaView&gt;</pre><p>In order to track component changes when making a request, we should create news as a useState hooks variable and calling the <em>setNews</em> function when we receive a response successfully.</p><pre>const [news, setNews] = useState([]);<br>.then(async function (response) {<br>  setNews(response.data);<br>})</pre><p>When we running the app after rendering the components we should use <em>useEffet</em> hooks and inside it, we will make the request to reflect the changes on the screen like this:</p><pre>useEffect(() =&gt; {<br> getNewsFromAPI();<br>}, []);</pre><pre>function getNewsFromAPI() {<br> axios.get(baseURL, {<br>    params: {<br>    country: &#39;us&#39;,<br>    apiKey: &#39;aa6a097fb9fb4509958fdabd1942e6d1&#39;,<br>    },<br> })<br>  .then(async function (response) {<br>      setNews(response.data);<br>  })<br>  .catch((error) =&gt; console.log(error));<br>}</pre><p>We will show the result after we attached the full News.js file :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*B-eS4IE-n29Tr7v7W2dZEw.png" /><figcaption>News.js</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/828/1*7C_0f2sdbAmse4baMVwR-Q.png" /><figcaption>Results</figcaption></figure><h3>Conclusion</h3><p>In this article, we have learned how to implement restful API’s using GET() and Axios methods in our React native app example. We also have learned how to handle responses from these methods and use their data in the app.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4e506af9b80f" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>