Search code examples
node.jsreact-nativeaxiosyelp

Axios - Request failed with status code 400 error


I don't know why I am getting 400 bad request error when I'm trying to hit my yelp api.

Request failed with status code 400
- node_modules\axios\lib\core\createError.js:15:17 in createError
- node_modules\axios\lib\core\settle.js:16:9 in settle
- node_modules\axios\lib\adapters\xhr.js:52:6 in handleLoad
- node_modules\event-target-shim\dist\event-target-shim.js:818:39 in EventTarget.prototype.dispatchEvent
- node_modules\react-native\Libraries\Network\XMLHttpRequest.js:566:23 in setReadyState
- node_modules\react-native\Libraries\Network\XMLHttpRequest.js:388:25 in __didCompleteResponse
- node_modules\react-native\Libraries\vendor\emitter\EventEmitter.js:190:12 in emit
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:436:47 in __callFunction
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:111:26 in __guard$argument_0
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:384:10 in __guard
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:110:17 in __guard$argument_0
* [native code]:null in callFunctionReturnFlushedQueue

Here's the code where I'm creating axios instead with mentioned baseURL:

import axios from "axios";
const apiKey =
  "<API KEY HERE>";

export default axios.create({
  baseURL: "https://api.yelp.com/v3/businesses",
  headers: {
    Authorization: `Bearer ${apiKey}`
  }
});

I am making search request for a particular term from another component here called SearhcScreen:

import { View, Text, StyleSheet } from "react-native";
import SearchBar from "../components/SearchBar";
import yelp from "../api/yelp";

const SearchScreen = () => {
  const [term, setTerm] = useState("");
  const [results, setResults] = useState([]);

  const searchApi = async () => {
    try {
      const response = await yelp.get("/search", {
        params: {
          term,
          limit: 50,
          location: "karachi"
        }
      });
      setResults(response.data.businesses);
    } catch (e) {
      console.log(e);
    }
  };
  return (
    <View>
      <SearchBar
        term={term}
        onTermChange={newTerm => setTerm(newTerm)}
        onTermSubmit={() => searchApi()}
      />
      <Text>Search Screen</Text>
      <Text>We have found {results.length} results.</Text>
    </View>
  );
};

const styles = StyleSheet.create({});

export default SearchScreen;

I have already checked that my API key that yelp provided is correct and also verified my developer account there. But still no luck in running / retrieving list from yelp api through axios yet.


Solution

  • Yelp.com not able to find your location karachi. So they return LOCATION_NOT_FOUND error according to your get request.

    You can test that by changing your location to New York.

    yelp.get("/search", {
      params: {
        term,
        limit: 50,
        location: "New York"
      }
    })
    

    So provide your latitude & longitude according to your location to specifying a more exact location. Make sure to provide those values as decimal

    yelp.get("/search", {
      params: {
        term,
        latitude: 24.86,
        longitude: 67.01,
        limit: 50,
      }
    });
    

    Hope this helps you. Feel free for doubts.