Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
When trying to make a connection to the API, I get the below error:
TypeError [ERR_INVALID_CHAR]: Invalid character in header content ["Authorization"]
Note that I have verified the API key (JWT) does not have any carriage returns, new lines, or tabs. Testing the API key and endpoints in Postman is successful.
Code (and test verifying key is free of carriage returns, new lines, and tabs):
console.log(/\t\r\n/.test(api_config.API_KEY)); // test to determine if key has carriage returns, new lines, or tabs
const connectionConfig = {
headers: {
'Authorization': 'Bearer ' + api_config.API_KEY
}
}
axios.get(api_config.BASE_URL + api_config.CAMPUSES_OPTIONS_URI, connectionConfig)
.then((response) => {
console.log(response.data);
})
.catch((err) => {
console.log(err)
})
Have you tested the API with Postman?
If that's work, you can easily create the code snippet based on your API with headers, body, etc.
Try to add more description in header
const connectionConfig = {
headers: {
'Authorization': 'Bearer ' + api_config.API_KEY,
'X-Requested-With': 'XMLHttpRequest'
}
}
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 days ago.
Improve this question
I ran into a problem when trying to do a fetch response in Javascript. I am getting an error "ReferenceError ReferenceError: headers is not defined" evertime I try to run it I get the same error. I am trying to submit a form with contact information I will not add that on here since it is long. Here is the Javascript code I don't know where in the code it is not defining the header.
const thisForm = document.getElementById('myForm');
thisForm.addEventListener('submit', async function(e) {
e.preventDefault();
let headers = new fetch.Headers();
headers.set('Authorization', 'Basic can\'t put this on here');
const response = await fetch("url", {
method: 'POST',
headers: {
headers,
body: JSON.stringify(Object.fromEntries(formData))
}
})
.then(response => response.json())
.then(json => console.log(result));
});
<form id=myForm><button>submit</button></form>
I have tried to add let headers = new fetch.Headers(); That did not work I am pretty sure I wrote everything else correctly
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 months ago.
Improve this question
I send JSON object using fetch() this way:
async testPost(){
const url = 'https://untitled-0clyb6aowq2u.runkit.sh';
var payload = { "test" : 1 };
const settings = {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
body: JSON.stringify(payload)
}
};
try {
const resp = await fetch(url, settings);
const data = await resp.json();
if(data.ok != 1){
console.log('FATAL ERROR PIZDEC: ');
console.log(data);
return { ok: 0, error: data};
}
console.log('Success:' + data.success);
return;
}
catch (e) {
return { ok: 0, error: e };
}
}
Server always gets the request, but there's just an empty object in the body. I've tried:
To use runkit as a server [fail]
To use express on localhost as a server [fail]
Check endpoints with the same request made from Postman. It works, postman sends normal json {"test" : 1}, it is printed in server console. So I conclude that the problem is on the client's side.
Check all the headers and CORS policy [doesn't help]
Use different approaches to send request: jQuerry.ajax(), XHR, construct Request() manually. [fail]
Why can the body just disappear?
I don't believe my question is a duplicate of this.
You shouldn't put body inside headers, see example on MDN here.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 months ago.
Improve this question
I am new to Express.js and am trying to create a URL shortener.
When the user naviagtes to domain.com/r/ I query the DB for the param and get the actual URL.
I use res.redirect to try to redirect to their stored URL but it fails with 'res.redirect is not a function'.
Please see the snippet below:
router.get('/r/:shortUrl', function(req, res) {
connectDatabase.then(
checkDatabase(req.params.shortUrl)
.then( res => {
console.log('checkdatdabase res => ' + res); //res = 'https://www.google.com'
res.redirect(res); //TypeError: res.redirect is not a function
})
.catch(e => {
//executed due to above error
console.error(e);
res.redirect(307, '/home');
})
)
});
Any advice would be much appreciated as this is a learning project for me.
You have two variables named res.
One, passed to the callback you pass to get() is a response object which has a redirect method.
The other is the resolved value of checkDatabase and is not a response object.
The latter has shadowed the former so you can't access it any more.
Use unique names for your variables to avoid this problem.
I recommend using a linter. ESLint can catch this kind of problem with the no-shadow rule.
datdabaseRes and res should be different variable name. Otherwise res has the respose object of database connection in the scope you are trying to use it.
router.get('/r/:shortUrl', function(req, res) {
connectDatabase.then(
checkDatabase(req.params.shortUrl)
.then( datdabaseRes => {
console.log('checkdatdabase res => ' + datdabaseRes); //res = 'https://www.google.com'
res.redirect(datdabaseRes); //TypeError: res.redirect is not a function
})
.catch(e => {
//executed due to above error
console.error(e);
res.redirect(307, '/home');
})
)
});
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
For my node.js express app, I'm using Request module to request data over REST API.
This is my request implementation in app.js
var request = require('request')
request.post('https://getpocket.com/v3/get', {
headers: {'content-type':'application/json'},
body: JSON.stringify({
consumer_key:'...',
access_token:'...',
contentType:'article',
sort:'title'
})
}, function (err, res, body) {
console.log(JSON.parse(body))
})
And I'm getting JSON response as the following
{ status: 1,
complete: 1,
list:
{ '890245271':
{ item_id: '890245271',
resolved_id: '890245271',
given_url: 'http://davidwalsh.name/open-graph-data-nodejs',
given_title: 'Get Open Graph Data with Node.js',
... }
},
error: null,
search_meta: { search_type: 'normal' },
since: 1444630917 }
The problem is that this is invalid JSON as JSON properties need to be string. What am I missing here?
You're parsing the JSON:
console.log(JSON.parse(body))
// Here ----^^^^^^^^^^^^^^^^
So what you're seeing is the console.log representation of the JavaScript object resulting from parsing the JSON.
If you want to see the JSON instead, just don't parse:
console.log(body);
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I'm new to this streaming/http long living connection stuff.
This is what I got so far:
var accessToken = require('./config.js').accessToken
, https = require('https')
;
var req = https.request({
host: 'alpha-api.app.net',
path: '/stream/0/posts/stream/global',
port: 443,
method: 'GET',
headers: {
'Authorization': 'Bearer ' + accessToken,
}}).on('response', function(response) {
response.on('data', function(chunk) {
console.log(chunk);
})
});
req.end();
req.on('error', function(e) {
console.error(e);
})
I was expecting this to run as long as possible and fetching updates as they drop in. But It turns out this terminates after a couple of seconds.
What am I missing?
Streams are not yet implemented in App.net see https://github.com/appdotnet/api-spec/blob/master/resources/streams.md
The API call you're making is only for the 20 most recent posts in the global stream: https://github.com/appdotnet/api-spec/blob/master/resources/posts.md#retrieve-the-global-stream