0

This is my API URL, so now I want to call this after re-render happens, I am completely new to reactjs, so I need basic knowledge on how to call API using fetch/axios according to the API URL? Expecting positive response.

http://newsapi.org/v2/everything?q=tech&apiKey=008c2c412a2b403698bc29a732374513&pageSize=10&page=1

1

2 Answers 2

0

Create a reusable component for your HTTP client service.

// httpService.js 
import axios from "axios"
axios.interceptors.response.use(null, error => {
    const expectedError =
        error.response &&
        error.response.status >= 400 &&
        error.response.status < 500;
    if (!expectedError) {
        console.log("Logging the error", error);
        alert("An unexpected error occurred");

    }
    return Promise.reject(error);
});
export default {
    get: axios.get,
    post: axios.post,
    put: axios.put,
    delete: axios.delete
};

Below is an example of a registration post request to the user information

// authService.js 

import http from "../services/httpService"
import { apiUrl } from "../../config.json"
const apiEndPoint = apiUrl + "";
export function register(user) {
  return http.post(apiEndPoint, {
    firstname: user.firstname,
    secondname: user.secondname,
    email: user.email,
    phone: user.phone,
    password: user.password,
  });
}

N.B. Config.json file contains your API URL.

Below is an example of a registration form component that has a doSubmit method for calling the server and navigating to a login page after a successful registration.

...

  doSubmit = async () => {
    // Call Server
    try {
      await userService.register(this.state.data);
      this.props.history.push("/Login"); // Programmatic Routing
    } catch (ex) {
      if (ex.response && ex.response.status === 400) {
        const errors = { ...this.state.errors };
        errors.username = ex.response.data;
        this.setState({ errors });
      }
    }
....

This example is made for illustration purposes, and it could be modified accordingly to your API requests.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks for your response. i have a doubt, for suppose "newsapi.org/v2/…" if this url contains some info like author and description. if i want to make an api call for that, can you give me some small example on it.
-2

Try this:

import React, { Component } from 'react';

class YourComponent extends Component {
    state = {}

    componentDidMount() {
        Your api call here...
    }

    render() {
        return (
             Your Codes Here...
        )
    }
}

export default YourComponent;

For Axios API call: https://youtu.be/4uzEUATtNHQ

For Fetch API call:
https://youtu.be/cuEtnrL9-H0

For Learning React: https://www.youtube.com/playlist?list=PL4cUxeGkcC9ij8CfkAY2RAGb-tmkNwQHG

2 Comments

thanks for your response. i have a doubt, for suppose "newsapi.org/v2/…" if this url contains some info like author and description. if i want to make an api call for that, can you give me some small example on it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.