ERROR - TypeError: Type error only on Safari after made GET request to the api server inside angular 5 app - node.js

So I am making the web app on top of angular 5.
The problem is when I try to call the api endpoint from the server.
When I get an error response (400+), it seems like on Safari it always throws and breaks the app.
ERROR - TypeError: Type error
ERROR CONTEXT – DebugContext_ {view: Object, nodeIndex: 0, nodeDef: Object, …}
But on Chrome it can handle the error correctly like this.
GET https://api.xxxx.com/projectname/v1.0/validation/access-token 400 (Bad Request)
This is my source code
const baseUrl = environment.apiUrl;
const fullUrl = baseUrl + '/productname/v1.0/validation/access-token';
const headers = new HttpHeaders({
'X-Access-Token': access_token
});
this.http.get(fullUrl, {
headers: headers
}).subscribe((res: any) => {
console.log(res, 'http res');
},
(error: any) => {
console.log(error, 'http err');
});
Anyone has an idea how to fix this?

After a lots of debugging, I figured out the issue that was causing for me. In every request, I was sending the name of the mobile device and for my case it was, Shashank's iPhone.
Calling setRequestHeader with single quote in the header was failing and hence all the HTTP request broke.

I had a Flutter App using an Inapp_Webview that points to an Angular 1.X web application and was also receiving a TypeError for all XHR requests after changing my iOS device name to include an apostrophe.
(*wanted to leave a comment, but didn't have the rep yet)

Related

HTTPS request by Axios got 504 error status on the website(live and local) but got 200 status in the chrome extension(app)

I have a project for a website and chrome app.
Following is a part of my code.
const payload = {
item1: 'item1',
item2: 'item2'
};
const url = 'https://api.example.com/api/login/login-callback?uid=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa&exp=1673882023';
const authRequest = await axios.post(url, payload);
I've built it for chrome extension, it works well.
And then I've deployed it to my localhost, it returns 504 error status. On the live site, it is same too.
I tried to solve it in several ways, but didn't find the key.
I'd like to hear experts' opinion.

SendGrid API v3 using NodeJS #sendgrid/client - Call is Unauthorized / API Key is valid with full permissions

I'm really struggling with a problem where every call I make to the #sendgrid/client comes back unauthorized. The exact same key can be used with #sendgrid/mail in the same code and it works fine.
I've tried everything but cannot get the NodeJS #sendgrid/client to work. I've even tried a brand new API Key and still get unauthorized.
I'm trying to use the /suppressions API. I can make the exact same call, with the same API key, in Postman and it works fine.
What am I doing wrong here?
Here is my code:
sgApiKey = config.get('sendGrid_APIKey');
sgClient.setApiKey(sgApiKey);
const email = "test#abc.com";
const headers = {
"on-behalf-of": "The subuser's username. This header generates the API call as if the subuser account was making the call."
};
const request = {
url: `/v3/asm/suppressions/global/${email}`,
method: 'GET',
headers: headers
}
sgClient.request(request)
.then(([response, body]) => {
console.log(response.statusCode);
console.log(response.body);
})
.catch(error => {
console.error(error);
});
I have tried creating a new API key with full permissions and it still does not work.
I'm expecting that my existing API key I already use for emailing should work to all the /suppression API.

How do I use the the post method with fetch and koa?

This is a function on my front-end that makes the request.
function postNewUser(){
fetch(`http://12.0.0.1:8080/users/test`, {
method: 'POST',
body: {nome: name, email: "test#test.com.br", idade: 20}
})
}
This is my back-end code to receive the request.
router.post('/users/:id', koaBody(), ctx => {
ctx.set('Access-Control-Allow-Origin', '*');
users.push(ctx.request.body)
ctx.status = 201
ctx.body = ctx.params
console.log(users)
})
For some unknown reason I receive nothing. Not even a single error message. The "console.log()" on the back-end is also not triggered, so my theory is that the problem is on the front-end.
Edit
As sugested by gnososphere, I tested with Postman, and it worked. So now i know the problem must be on the fron-end code.
You can try your backend functionality with Postman. It's a great service for testing.
the request would look something like this
If the problem is on the frontend, double check your fetch method by posting to a website that will return data and logging that in your app.

Node-soap - Unable to decoding Gzip encoded response

I am using node-soap library to make some requests on my Nest app.
In order to create the client, I am using the following code:
const client = await soap.createClientAsync(requestURL, {
wsdl_options: {
timeout: 600000,
},
});
client.addHttpHeader('Connection', 'Keep-Alive');
client.addHttpHeader('Accept-Encoding', 'gzip,deflate');
And, this one as an example of the request:
const providerResponse = await client.doAvailableProductsAsync(request);
return providerResponse[0]
This code is working fine when I remove the client.addHttpHeader('Accept-Encoding', 'gzip,deflate');, but with it, is throwing the following exception:
{
faultcode: 500,
faultstring: 'Invalid XML',
detail: 'Error: Non-whitespace before first tag.\nLine: 0\nColumn: 1\nChar: \u001f',
statusCode: 500
}
Furthermore, the exception shows an encoded text with special chars. I guess, the header is working fine, but my node-soap client is not able to decode the response properly from the server side.
Am I missing any soap-related configuration?
I read the docs carefully and I could not find any reference to request compression.
Thanks in advance.

How to open web brower by using AWS post lambda

I have written the piece of code below:
static async postSearchResult(httpContext: HttpContext, injector: Injector) {
const log = injector.get(Log);
const service = injector.get(Service);
try {
let result = await service.redirectToUI(JSON.parse(httpContext.getRequestBody()));
httpContext.ok(result, 200, {'Content-Type': 'application/json'});
} catch (e) {
httpContext.fail(e, 500);
}
}
protected redirectToUI(response: any) {
// If any post api call happened then it should open web browser and pass some field as query parameter
window.open("https://www.google.com?abc=response.abc");
return response ? response : "failed";
}
Here I am getting the following error :
Execution failed ReferenceError: Window is not defined
What am I doing wrong?
What you are trying to accomplish doesn't make much of a sense. Lambda is a back-end service. To open new browser window, you need to use front-end JavaScript, not back-end Node (on the back-end, you have no access to the front-end window object).
If you want to open a new browser window as a reaction to some back-end response, then you can send some indicator in the HTTP response (i.e shouldOpenNewWindow: true as a part of the response object), parse that response on the front-end and it the indicator is present, then you can issue window.open command. But it has to be done on front-end.

Resources