使用 axios 在 react-native 中使用 POST 方法获取 API 数据

Get API data with POST method in react-native with axios

我需要使用 axios 在 react-native 应用程序中获取数据。我可以使用如下简单的 GET 方法获取数据:

class AlbumList extends Component {

  state = { albums: [] };

componentWillMount() {
  //axios.get('https://rallycoding.herokuapp.com/api/music_albums') .then(response => console.log(response));
  axios.get('http://rallycoding.herokuapp.com/api/music_albums')

  .then(response => this.setState({ albums: response.data }));
        }
        renderAlbums() {
          return this.state.albums.map(album =>
          <AlbumDetail key={album.title} album={album} />);
         // return this.state.albums.map(album => <Text>{album.title}</Text>);
        }

render() {
    console.log(this.state);

return (
   <View>
        {this.renderAlbums()}
    </View>    
);
}
}

如何使用 POST 方法从 API 获取数据,我还需要传递 header 和 api-username、api-password、apitoken?

我需要类似 但需要 AXIOS.

编辑:

我需要从 LINNWORK API. if someone had done this please guide. They first need to authorize 获取数据,然后我可以从那里获取数据。所以授权然后下一步。

axios post 方法接受 3 个参数,即 url、数据和配置。

您可以按如下方式构造 axios post 请求:

axios.post(
    'http://rallycoding.herokuapp.com/api/music_albums', 
    {
       'param1': 'value1',
       'param2': 'value2',
       //other data key value pairs
    },
    {
       headers: {
           'api-token': 'xyz',
            //other header fields
       }
    }
);

在您的情况下,您需要访问 https://api.linnworks.net/api/Inventory/GetCategories,根据 docs 需要 token 来自 Authorization header 中的 auth api .所以你通过 axios 的 GET 请求将是:

axios.get("https://api.linnworks.net/api/Inventory/GetCategories", { 
    headers: {
      'Authorization': 'token-from-auth-api'
    }
}).then((response) => {
    console.log(response.data);
})