skip to Main Content

Here is my code which is causing the problems. I have Axios and I don’t know how to use it effectively.

import axios from "axios";

const options = {
  headers: {
    'content-type': 'application/json',
    ContentType: 'text/json; charset=utf-8',
    'X-RapidAPI-Key': 'REACT_APP_KEY',
    'X-RapidAPI-Host': 'rdrunnerxx-trackservice.p.rapidapi.com'
  },
  data: '{"Locations":[{"LatLong":{"Latitude":39.767968, "Longitude": 64.421725}},{"LatLong":{"Latitude":40.022882 , "Longitude": 64.516878}}],"RouteOptions":{"DistanceUnit":0,"RouteOptimize":0,"Culture":"en-US","MapSize":null,"RouteColor":null}}'
};


export const ApiService = {
    async fetching() {
        const response = axios.post('https://rdrunnerxx-trackservice.p.rapidapi.com/route', data,  { options })
        return response
    },
}

const response = axios.post('url', data,  { options })
const data = ?

2

Answers


  1. i think you need edit ContentType to "Content-Type":"application/json"

    const {data} = await axios.post('/user', document.querySelector('#my-form'), {
      headers: {
        'Content-Type': 'application/json'
      }
    })
    

    document : https://axios-http.com/docs/post_example

    Login or Signup to reply.
  2. This should work

    async function fetching() {
      try {
        const response = await axios.post(
          'https://rdrunnerxx-trackservice.p.rapidapi.com/route',
          options.data,
          { headers: options.headers }
        );
    
        const data = response.data;
        console.log('data', data);
        return data;
      } catch(error) {
        console.log(error)
      }
    }
    
    export const ApiService = {
       fetching,
    }
    
    

    To use the Fetching function

    // component.jsx
    
    useEffect(() => {
      fetching();
    }, []) 
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search