I am making a GET request in my react native app. My code is:
const config = {
headers: {
Authorization: `Bearer ${authToken}`,
},
};
axios.get(url, config);
The authorization token is not being sent along with the request. How do I send it?
You could use the same GET method in two ways.
Method 1
axios.get(your_url, {
headers: {
Authorization: 'Bearer ' + your_token
}
})
Method 2
axios({
method: 'get',
url: your_url,
headers: {
Authorization: 'Bearer ' + your_token
}
})
For POST method
axios({
method: 'post',
url: your_url,
data: { "user_id": 1 }
headers: {
Authorization: 'Bearer ' + your_token
}
})
Please try with the any of above method and let me know.
try this:
var req = {
url: the url ,
method: "get",
headers: {
Authorization: "Bearer " + val,
Accept: "application/json"
}
};
axios(req)
.then(response=>{console.log(response)})
.catch(error => {});
});
Related
I am trying to replace a fetch with axios. I keep getting undefined in my console log.
async componentDidMount() {
console.log('app mounted');
const tokenString = sessionStorage.getItem("token");
const token = JSON.parse(tokenString);
let headers = new Headers({
"Accept": "application/json",
"Content-Type": "application/json",
'Authorization': 'Bearer ' + token.token
});
const response = await axios({
method: 'get',
url: Config.apiUrl + `/api/Orders/GetAllInvoices`,
headers: {
"Accept": "application/json",
"Content-Type": "application/json",
'Authorization': 'Bearer ' + token.token }
});
console.log(`axios: ${response.json}`)
this.setState({ invoiceList: response.json });
//const response = await fetch(Config.apiUrl + `/api/Orders/GetAllInvoices`, {
// method: "GET",
// headers: headers
//});
//const json = await response.json();
//console.log(json);
//this.setState({ invoiceList: json });
...
... the commented out fetch is working. I just now added the .json even though axios should not need it. Neither way works. What am I doing wrong?
Did you even console.log(response) just to see whats inside of it?
I guess you dont, because response is an object witch has no json key in it. You should use response.data
I am trying to do a post request with axios but it does not work.
This is the request I want to do.
I tried like this:
await axios.post(
'https://api.searchads.apple.com/api/v4/campaigns/XXXX/adgroups/targetingkeywords/find',
{
headers: {
"Authorization": "Bearer " + accessToken,
"X-AP-Context": "orgId=XXXX"
},
data: data
});
try this
var config = {
method: 'post',
url: 'https://api.searchads.apple.com/api/v4/campaigns/{campaignId}/adgroups/targetingkeywords/find',
headers: {
'Content-Type': 'application/json'
},
data: data
};
axios(config)
.then(function (response) {
console.log(JSON.stringify(response.data));
})
.catch(function (error) {
console.log(error);
});
I don't want to repeat headers for all request.
async function fetchA() {
return await axios({
method: 'GET',
url: API_URL + `/api/a`,
headers: {'Authorization': 'Bearer ' + localStorage.getItem(ACCESS_TOKEN)}
});
}
async function fetchAById(id) {
return await axios({
method: 'GET',
url: API_URL + `/api/a/${id}`,
headers: {'Authorization': 'Bearer ' + localStorage.getItem(ACCESS_TOKEN)}
});
}
I assigned headers to const but in this case accessToken was null.
What is best way to create function which adds accessToken itself?
function getHeaders() {
const serviceInstance = axios.create({
url: API_URL + `/api/a`,
headers: {'Authorization': 'Bearer ' + localStorage.getItem(ACCESS_TOKEN)}
});
return serviceInstance;
}
async function fetchA() {
return await getHeaders().get(url)
});
}
I hope this may help.
I'm trying to make a request to get an authorization code from the spotify api using fetch but I keep getting a 415 error code. I did not have any errors when i was originally using $.ajax instead of fetch.
let client_id = '8f10fa8af1aa40c6b52073811460bf33'
let client_secret = '27a7c01912444b409a7f9a6d1f700868'
let ah = btoa(client_id + ":" + client_secret)
const getAuthToken = (searchedTerm) => {
fetch( `https://accounts.spotify.com/api/token`,
{
headers: {
'Content-Type': 'application/x-www-form-url-encoded',
'Authorization': `Basic ${ah}`
},
body: {
grant_type: 'client_credentials'
},
json: true,
method : "POST"
}
)
.then(function(response) {
authToken = response.access_token;
spotifySearch(response.access_token, searchedTerm);
})
}
See this answer on a similar post. Note that there they set 'Content-Type':'application/x-www-form-urlencoded', with no hyphen between url and encoded. I think you simply need to change
headers: {
'Content-Type': 'application/x-www-form-url-encoded',
'Authorization': `Basic ${ah}`
},
to
headers: {
'Content-Type': 'application/x-www-form-urlencoded', // no hyphen in urlencoded
'Authorization': `Basic ${ah}`
},
I am trying to configure headers in a project with vue.js and axios to call a service that expects a json. My problem is that when I make my call with the POST method, axios puts to the request Content-Type header with x-www-urlencoded, but in my code, I put manually Content-Type header with application / json.
var loginObj = {
var1: payload.login,
var2: payload.password
}
const jsonLogin = JSON.stringify(loginObj)
const config = {
headers: {
'content-type': 'application/json',
'Accept': 'application/json'
}
}
axios({
url: 'url/example',
method: 'post',
data: jsonLogin,
config
})
.then(response => {
console.log(response);
})
.catch(error => {
console.log(error);
})
The Axios property to set header config should be headers: and you are setting with config:
try with:
axios({
url: 'url/example',
method: 'post',
data: jsonLogin,
headers: config.headers
})
or change your const config to:
const configHeaders = {
"content-type": "application/json",
"Accept": "application/json"
};
and use it with:
axios({
url: "url/example",
method: "post",
data: jsonLogin,
headers: configHeaders
});