Login token Axios post request gets Unauthorized response - reactjs

I am encountering some difficulties while trying to update some data in a mysql database.
I am making an axios post request towards this api and inside the header I am sending the token that I receive from the backend after succesfull login.
I will show here the apiDocumentation and the POST req that I am making because I really don't understand how to send the login token to have authorization to the data.
Any help would very helpfull.
This is the backend apiDocumentation:
"post": {
"summary": "Post new data row",
"description": "Post new data row",
"parameters": [
{
"name": "token",
"in": "header",
"required": true,
"description": "token",
"schema": {
"type": "string"
}
}
],
"requestBody": {
"required": true,
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"data": {
"type": "string",
"example": "{\"test\":1}"
},
"type": {
"type": "string",
"example": "1"
},
"status": {
"type": "integer",
"example": 1
}
},
"required": [
"data",
"type"
]
}
}
}
},
This is my Post request that I am doing in my code:
case "newsTitle" :{
const {titlu} = this.state;
data = JSON.stringify(titlu);
const token = this.props.history.location.state.token;
const headers = {
//"token":token
"Authorization": token,
'Accept': '*/*',
//"Content-Type": "multipart/form-data"
"Content-Type":"application/json"
};
try {
const response = await axios.post(url, data, headers);
console.log("response::", response);

try this
case "newsTitle" :{
const {titlu} = this.state;
data = JSON.stringify(titlu);
const token = this.props.history.location.state.token;
const headers = {
//"token":token
'Authorization': `Bearer ${token}`
'Accept': '*/*',
//"Content-Type": "multipart/form-data"
"Content-Type":"application/json"
};
try {
const response = await axios.post(url, data, headers);
console.log("response::", response);

Related

Getting CORS Error while Integrating FedEx with React Js

While working on FedEx integration with React, we are able to get authorization key using Google extension but unable to work on other APIs. All the APIs are perfectly working in postman but not while integration. Tried checking rates and transit times but getting CORS error. I have included the code and error screenshot.Could you please help?
let body = JSON.stringify({
"accountNumber": {
"value": "XXXXXXXXX"
},
"requestedShipment": {
"shipper": {
"address": {
"postalCode": 75063,
"countryCode": "US"
}
},
"recipient": {
"address": {
"postalCode": "L0J 0A0",
"countryCode": "CA"
}
},
"pickupType": "DROPOFF_AT_FEDEX_LOCATION",
"serviceType": "FEDEX_GROUND",
"rateRequestType": [
"LIST",
"ACCOUNT"
],
"customsClearanceDetail": {
"dutiesPayment": {
"paymentType": "SENDER",
"payor": {
"responsibleParty": null
}
},
"commodities": [{
"description": "Camera",
"quantity": 1,
"quantityUnits": "PCS",
"weight": {
"units": "KG",
"value": 20
},
"customsValue": {
"amount": 100,
"currency": "USD"
}
}]
},
"requestedPackageLineItems": [{
"weight": {
"units": "KG",
"value": 20
}
}]
}
});
axios.post("https://apis-sandbox.fedex.com/rate/v1/rates/quotes", body, {
headers : {
"Accept": "*/*",
"Content-Type": "application/json",
"authorization": `Bearer XXX`,
"Access-Control-Allow-Origin": "*",
}
}).then(res => console.log(res.data));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
Cors error cannot be solved from front end , cause its a browser error. so You have to create proxy server to solve this
create an API in backend and send request from server to fedex.

unauthorized with sendgrid smtp

I'm sending emails using sendgrid smtp and always get unauthorized error for the request.
I followed the docs here https://sendgrid.com/docs/for-developers/sending-email/api-getting-started/.
Should I login in from my terminal or something like this?
const sendFunc = () => {
return fetch('https://api.sendgrid.com/v3/mail/send', {
method: 'Post',
mode: 'no-cors',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer {my apiKey-here}'
},
data: { "personalizations": [{ "to": [{ "email": "mahmoudnafea19#gmail.com", "name": "John Doe" }], "subject": "Hello, World!" }], "content": [{ "type": "text/plain", "value": "Heya!" }], "from": { "email": "mahmoud.m.elsayed19#gmail.com", "name": "Sam Smith" }, "reply_to": { "email": "sam.smith#example.com", "name": "Sam Smith" } }
}).then((response) => console.log(response)).catch((err) => console.log(err))
}
I had to pass the data and headers as objects outside the function call

Users added via graph not receiving welcome email

First I tried adding a group owner as both owner and member in the same http call as I created the group:
{
"description": "test group",
"displayName": "test_group",
"groupTypes": [
"Unified"
],
"mailEnabled": true,
"mailNickname": "project.1-TestPreview",
"members#odata.bind": [
"https://graph.microsoft.com/v1.0/users/{id}"
],
"owners#odata.bind": [
"https://graph.microsoft.com/v1.0/users/{id}"
],
"securityEnabled": false,
"visibility": "private"
}
The user is added as a member and owner successfully, but not receiving a welcome email.
I then tried to split the http calls, thinking that there was an issue with the emails as the group was created at the same time as the user was added.
I created the group with one user being added in the creation http call same as above and three other users being added as owner and member ~30 seconds after the group was created. For this I used
queries:
{
"type": "POST",
"url": "https://graph.microsoft.com/v1.0/$batch"
}
body
{
"requests": [
{
"body": {
"#odata.id": "https://graph.microsoft.com/v1.0/users/{id}"
},
"headers": {
"Content-Type": "application/json"
},
"id": "1",
"method": "POST",
"url": "groups/{id}/members/$ref"
},
{
"body": {
"#odata.id": "https://graph.microsoft.com/v1.0/users/{id}"
},
"headers": {
"Content-Type": "application/json"
},
"id": "2",
"method": "POST",
"url": "groups/{id}/members/$ref"
},
{
"body": {
"#odata.id": "https://graph.microsoft.com/v1.0/users/{id}"
},
"headers": {
"Content-Type": "application/json"
},
"id": "3",
"method": "POST",
"url": "groups/{id}/members/$ref"
}
]
}
I added the users as owners with the same method. None of them received a welcome email. How can I make sure that the email fires?
I am doing this in Logic Apps:
You need a proper Microsoft 365, Office 365 or Exchange license. Do you have one?

Different response on similar requests Gmail API

I'm trying to handle the response object from Gmail API with getting attachments from certain messages:
In my Gmail account i have 2 Labels:
Label_1
Label_2
both messages get daily reports from different web services and im using the Gmail API to first get a list of messages while im filtering a message with a Label and a query search string (q field) which contains a certain date (this is how i get only one message when i request a list)
with the response object Im able to get the message ID and then send another request with the message ID.
I want to download the attachments so in the response object im searching for the Payload.Parts field of the response object
In Label_1 response - Payload.Parts[0] contains the filename field and Payload.Parts[0].Body contains the attachmentID
In Label_2 response - Payload.Parts[1].Parts[0] contains the filename and same for .Body contains the attachmentID.
my question is: why this is happening?
why in one response i get the wanted fields in the first Payload.Parts and in the second i have to go deeper in the object's fields?
I've also noticed that in Label_1 response I receive an HTTP header which I dont get on the second one that I think may be related: DKIM-Signature
any thoughts?
Thanks.
The response you get is just the RFC822-message parsed to JSON. The message might be multipart/mixed, multipart/related, text/html or something similar. It is best to write your code so you check all parts in the payload:
var response = {
"payload": {
"parts": [
{
"mimeType": "multipart/alternative",
"filename": "",
"headers": [
{
"name": "Content-Type",
"value": "multipart/alternative; boundary=001a1142e23c551e8e05200b4be0"
}
],
"body": {
"size": 0
},
"parts": [
{
"partId": "0.0",
"mimeType": "text/plain",
"filename": "",
"headers": [
{
"name": "Content-Type",
"value": "text/plain; charset=UTF-8"
}
],
"body": {
"size": 9,
"data": "V293IG1hbg0K"
}
},
{
"partId": "0.1",
"mimeType": "text/html",
"filename": "",
"headers": [
{
"name": "Content-Type",
"value": "text/html; charset=UTF-8"
}
],
"body": {
"size": 30,
"data": "PGRpdiBkaXI9Imx0ciI-V293IG1hbjwvZGl2Pg0K"
}
}
]
},
{
"partId": "1",
"mimeType": "image/jpeg",
"filename": "feelthebern.jpg",
"headers": [
{
"name": "Content-Type",
"value": "image/jpeg; name=\"feelthebern.jpg\""
},
{
"name": "Content-Disposition",
"value": "attachment; filename=\"feelthebern.jpg\""
},
{
"name": "Content-Transfer-Encoding",
"value": "base64"
},
{
"name": "X-Attachment-Id",
"value": "f_ieq3ev0i0"
}
],
"body": {
"attachmentId": "ANGjdJ_2xG3WOiLh6MbUdYy4vo2VhV2kOso5AyuJW3333rbmk8BIE1GJHIOXkNIVGiphP3fGe7iuIl_MGzXBGNGvNslwlz8hOkvJZg2DaasVZsdVFT_5JGvJOLefgaSL4hqKJgtzOZG9K1XSMrRQAtz2V0NX7puPdXDU4gvalSuMRGwBhr_oDSfx2xljHEbGG6I4VLeLZfrzGGKW7BF-GO_FUxzJR8SizRYqIhgZNA6PfRGyOhf1s7bAPNW3M9KqWRgaK07WTOYl7DzW4hpNBPA4jrl7tgsssExHpfviFL7yL52lxsmbsiLe81Z5UoM",
"size": 100446
}
}
]
}
};
// In e.g. a plain text message, the payload is the only part.
var parts = [response.payload];
var attachmentIds = [];
while (parts.length) {
var part = parts.shift();
if (part.parts) {
parts = parts.concat(part.parts);
}
if(part.body && part.body.attachmentId) {
attachmentIds.push(part.body.attachmentId);
}
}
console.log(attachmentIds);

Trying to post json in angularjs will 400 bad request

I am trying to post a static new entry using angularjs $http call but it will throws 400 bad request I am not getting why? Here I am passing the exact same type of json object in input.
Below is my main json file in which I want to add new record .
{
"count": 384,
"next": "http://104.197.128.152:8000/v1/tracks?page=2",
"previous": null,
"results": [
{
"id": 38,
"title": "Hey Jude",
"rating": "4.9",
"genres": [
{
"id": 5,
"name": "ramesh"
}
]
},
{
"id": 39,
"title": "hello adele",
"rating": "4.0",
"genres": [
{
"id": 4,
"name": "bollywood"
},
{
"id": 8,
"name": "metakai"
}
]
}
]
}
Data which I want to post:
var data = {
"id": 79,
"title": "new song anand",
"rating": "4.0",
"genres": [
{
"id": 4,
"name": "bollywood"
},
{
"id": 8,
"name": "metakai"
}
]
}
Syntax for posting data:
$http({
method: 'POST',
url: 'http://104.197.128.152:8000/v1/tracks',
dataType: 'json',
data: data,
headers: { 'Content-Type': 'application/json; charset=UTF-8' }
}).success(function (data) {
console.log(data,"success");
}).error(function (data) {
console.log(data,"fail");
});
Remove dataType: 'json' and charset=UTF-8 from headers.
$http({
method: 'POST',
url: 'http://104.197.128.152:8000/v1/tracks',
data: data,
headers: { 'Content-Type': 'application/json'}
}).success(function (data) {
console.log(data,"success");
}).error(function (data) {
console.log(data,"fail");
});
It must work .
And if not re-check your JSON format. Something must be missing.

Resources