使用POST方法获取带有axios的react-native的API数据

Jai*_*Jai 6 react-native axios

我需要使用axios在react-native app中获取数据.我可以使用简单的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>    
);
}
}
Run Code Online (Sandbox Code Playgroud)

如何从API获取带有POST方法的数据,我还需要传递头文件和api-username,api-password,apitoken?

我需要像/sf/answers/2895098041/这样的东西,但需要AXIOS.

编辑:

我需要从LINNWORK API 获取数据.如果有人这样做,请指导.他们首先需要授权,然后我可以从那里获取数据.所以authrize然后下一步.

Dee*_*eva 8

axios post方法有3个参数,即url,data&config.

您可以按如下方式构建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
       }
    }
);
Run Code Online (Sandbox Code Playgroud)

在您的情况下,您需要访问https://api.linnworks.net/api/Inventory/GetCategories,根据文档要求tokenAuthorization标头中的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);
})
Run Code Online (Sandbox Code Playgroud)