I need to send GET request on https url with "Authorization" header. I try to do this using this code:
request.get({
url: url,
headers: {
'Authorization': token,
'abc': 'def'
}
})
On server side I get "abc" header, but there is no Authorization header. Why does it happen?
Have you tried using auth key? See the details example in the project's readme
request.get('http://some.server.com/', {
'auth': {
'user': 'username',
'pass': 'password',
'sendImmediately': false
}
});
Related
So i use actix web on the backend and svelte on the frontend
This sends the cookie to the frontend
let cookie = auth_authority.create_signed_cookie(UserClaims {
id: o.id,
role: Role::User,
})?;
info!("Logged in user");
info!("{}", cookie);
Ok(HttpResponse::Ok()
.cookie(cookie)
.json("You are now logged in"))
And this is the CORS config:
let cors = Cors::default()
.allowed_origin("http://localhost:3000")
.allowed_methods(vec!["POST", "GET"])
.allowed_headers(vec![
header::AUTHORIZATION,
header::CONTENT_TYPE,
])
.expose_any_header()
.supports_credentials()
.max_age(3600);
And this is the function that fetches the data for svelte:
const resp = await fetch("http://0.0.0.0:4000/login", {
method: "POST",
mode: 'cors',
credentials: 'include',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(
{
username,
password,
}
)
})
I do not recieve the Set-Cookie header when using a web browser but when i use curl i do(Most likely caused by CORS). I can send other headers that i create but i cannot send Set-Cookie no matter what. I used the .supports_credentials() method to allow cookies but that did nothing.
Can someone guide me into a direction to solve this or does someone know how to solve it?
I am replacing the deprecated 'request' package of npm with 'axios' latest in Node 16.15.x application.
However, I am facing an issue with one POST request sent via axios (other POST requests I was able to implement)
Here is code using 'request' package POST
request.post('https://sampleserver6.arcgisonline.com/arcgis/tokens/generateToken', {
form: {
'username': 'user1',
'password': 'user1',
'client': 'requestip',
'expiration': 60,
'f': 'json',
}
}, (err, response, body) => {
console.log('****Token', body)
})
This gives the expected response. Which is a token. I am using a sample REST server "https://sampleserver6.arcgisonline.com/arcgis/tokens/generateToken"
Now when trying the same POST request using 'axios' POST,
axios.post('https://sampleserver6.arcgisonline.com/arcgis/tokens/generateToken', {
'username': 'user1',
'password': 'user1',
'client': 'requestip',
'expiration': 60,
'f': 'json',
}, {
headers: {
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
}
})
.then(function(response) {
console.log('****Token', body)
})
The response is 200 however the response data shows an error as "Invalid request Usage: https://sampleserver6.arcgisonline.com/arcgis/tokens?request=gettoken&username=username&password=password&"
This is the same error on the sample REST server as well when you put no values/partial values in the form and click 'Generate Token'.
So it seems 'axios' is not able to put form data as expected.
Am I using the correct implementation in 'axios' POST?
I have tried using the 'form-data' package as well -> same error
I have tried different Content-Type -> Issue persists
Using URLSearchParams instead of FormData worked for me.
const param = new URLSearchParams({
'username': 'user1',
'password': 'user1',
'client': 'requestip',
'expiration': 60,
'f': 'json',
});
axios({
method: 'post',
url: 'https://sampleserver6.arcgisonline.com/arcgis/tokens/generateToken',
data: param.toString()
});
P.S: Did not have to pass any headers.
I want to get a list of projects in MongoDB using its API "https://cloud.mongodb.com/api/atlas/v1.0/groups" but every time I get error as "401 You are not authorized for this resource".
According to the docs digest authentication is used.
Seems like I am passing the Private_key and Public_key in the wrong way.
Below is my request object
{
url: 'https://cloud.mongodb.com/api/atlas/v1.0/groups',
method: 'GET',
headers: {
'Accept': 'application/json',
},
auth: {
user: 'Public_Key',
pass: 'Private_key'
}
}
Can anyone please help me with this.
What you are missing is the key "sendImmediately". You need to send it in your auth object as follows :
request({
method: 'GET',
auth: {
"user": Public_Key,
"pass": Private_key,
"sendImmediately": false
},
url: 'https://cloud.mongodb.com/api/atlas/v1.0?pretty=true'
})
I'm using axios to perform get and post requests to an external api,
i have finally succeed to achieve get request (problem with the ssl certificate, i avoid it by adding this :
httpsAgent: new https.Agent({ rejectUnauthorized: false }),
now i would like to post the api,
to get the request working in postman, i put in
headers content-type : application/json
and in the body : {}
like here
when trying with a google chrome extention, to make it work, i put nothing in the headers but in params, i select customer : application/json and i put inside this {} instead of the default choice which is x-www-form-urlencoded;charset=UTF-8
chrome extention
in my javascript app i tried this
var url = https://10.11.31.100:9440/api/nutanix/v3/images/list;
axios({
method:'post',
httpsAgent: new https.Agent({ rejectUnauthorized: false }),
url,
auth: {
username: '******',
password: '********'
},
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'X-Requested-With': 'XMLHttpRequest'
},
params: {},
data: {}
})
.then(function (response) {
res.send(JSON.stringify(response.data));
console.log(response);
})
.catch(function (error) {
console.log(error);
});
I get this problem :
TypeError : UTF-8 is not a function
Specifically with regards to the Nutanix v3 REST API - this is probably because the POST request above doesn't have an appropriate JSON payload i.e. the "data" parameter is empty.
When sending Nutanix v3 API POST requests, specifically to "list" entities, you'll need to specify the "kind" of entity being listed. In your example, the JSON payload below will work.
{"kind":"image"}
See here: https://nutanix.dev/reference/prism_central/v3/api/images/postimageslist
HTH. :)
guys. I have a than error in my NodeJS rest API, and can't resolve this.
My idea is make a github login, this app working like this.
Href to github url returning a temporal code in callback.
Latter, send this temporal code to my REST API and with rest api make a fetch request to other endpoint of the github api, and this endpoint should return access_token=12345 (this access token is a example), for latter send this token to frontend, and convert the token in a JWT token and also send to frontend for latter storage in a localStorage to use it.
My code in NodeJS
router.post("/users/github/:code",function(req,res){
fetch('https://github.com/login/oauth/access_token/', {
method: 'GET',
client_id: 'xxxx',
client_secret: 'xxxx',
code: req.params.code,
accept: 'json',
})
.then(function(res) {
return res.json();
}).then(function(json) {
console.log(json);
});
});
PD: I use node-fetch module for this. https://www.npmjs.com/package/node-fetch
The solution
router.post("/users/github/:code",function(req,res){
fetch('https://github.com/login/oauth/access_token/', {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
client_id: 'xxxx',
client_secret: 'xxxx',
code: req.params.code
})
}).then(function(res) {
return res.json();
}).then(function(body) {
res.json(body);
});
});