How to send Token to API in react native - javascript

Backend developer send me API like this To test it on poastman
http://app/channel_partner/patients/add?token=59bd1e3711ce73c150b68f3741df4363cf766a0e2fe54dcb5f804a08f3f0d525
But now How can I send token to API in react native I am sending like this but its Not working
let url = URLs.addLeads;
let options = {
method: "POST",
url: url,
headers: {
Accept: "application/json",
"Content-Type": "application/json",
},

You've tagged this with axios so use the params config option to safely add URL query parameters to your request
const options = {
url: URLs.addLeads,
method: "POST",
params: { token }
};
This will add a token query parameter with a correctly URL-encoded value.
You don't need to change the headers; the Axios defaults already have you covered.

Related

Not receiving data on server when doing an API POST call from the client

Already checked the endpoint with Insomnia and is working fine, but when trying to connect with the backend from the client there is some kind of problem. The connection between the client and the server is done this way:
const uri = `${basePath}/${baseVersion}/sign-up`;
const params = {
method: "POST",
body: JSON.stringify(data),
header: {
"Content-Type": "application/json"
}
};
And if I show in the console params object this is what is inside it:
enter image description here
Just to clarify, there isn't a CORS problem as I am using a Google Chrome extension for it.
This is the response of the fecth:
enter image description here
Is your problem not receiving a response from the server in the promise? If so, that is because there is no code in your snippet that actually returns the data. (Sorry if I misidentified the problem, I don't have the ability to comment)
const uri = `${basePath}/${baseVersion}/sign-up`;
async function fetchPost(data = {}) {
var response = await fetch(uri,
method: "POST",
mode: "cors",
header: {
"Content-Type": "application/json"
},
referrerPolicy: "strict-origin-when-cross-origin" //you can replace that with anything you want depending on the situation
body: JSON.stringify(data)
});
// if you're expecting the response to be json, use the below, but if you want it in text, then do response.text, etc.
return response.json();
}
fetchPost();

How to download a file in react js with blob object and how does featherjs can help handle blob as 'responseType'?

If below format is used in axios calls , how can we achieve the same via feathersjs api call ?
Where do we need to specify the 'responseType' key ?
return axios( {
method: 'get',
url: '_URL_',
responseType: blob
} );
}
I tried mentioning below code block for post request via create method from feathersjs doc:
var config =
{
headers: { 'Content-Type': 'application/json'},
responseType: 'blob'
}
const response = _SERVICE_NAME_.create(data, config)
please help me to understand the format for mentioning responseType in request as a param in feathersjs

401 Unauthorized REST API in lightning web component salesforce

I'm trying to execute query using REST API, in a lightning web component.
the request in Postman returning result with success (enabling Follow Authorization header)
but in the JavaScript in lightning web component it returns 401 Unauthorized
the code in the java script is a follow :
let sessionId = 'tokken';
let baseUrl = window.location.origin;
let header = {
"Content-Type": "application/json",
"Accept": "application/json",
"Authorization": "Bearer " + sessionId,
};
if (sessionId) {
let options = {
method: "GET",
mode: 'no-cors',
redirect: 'follow',
headers: header,
};
fetch(baseUrl + '/services/data/v50.0/query/?q=SELECT+name+from+Account', options).then((response) => {
console.log(JSON.stringify(response));
if (!response.ok) {
// throw Error(JSON.stringify(response));
} else {
return response.json();
}
}).then((repos) => {
console.log(repos, repos);
});
}
am I missing something ?
Since you can not pass the value Authorization to no-cors mode, you will need to add CORS configuration in your SalesForce as safe endpoint where they let you make a call.
You can not send Authorization header with "no-cors" mode.
mode: "no-cors"only allows a limited set of headers in the request:
Accept
Accept-Language
Content-Language
Content-Type with a value of application/x-www-form-urlencoded, multipart/form-data, or text/plain

how to put a body in a get request?

i gonna send a get request, but i need to send with a body,
when i use a postman it works well, but if i use axios, it dosent't works.
i use axios in javascript
and i use postman
var settings = {
"url": "http://127.0.0.1:5000/user/history",
"method": "GET",
"processData": false,
"data": "{"summoner":"몽디로 맞아봐"}"
}
$.ajax(settings).done(function (response) {
console.log(response);
});
axios({
url: 'http://127.0.0.1:5000/user/history',
method: 'get',
data: {
'summoner': '몽디로 맞아봐'
}
});
i expect this code works
ES6:
import axios from 'axios';
let requestHeader, data;
data = {
'summoner': '몽디로 맞아봐'
}
requestHeader = {
'Content-Type':'application/json'
}
axios.get('http://127.0.0.1:5000/user/history', {
params: data,
headers: requestHeader
}).then((response) => {
console.log(response)
})
In HTTP specification, sending GET request with body is ok, you can try it with http module in nodejs.
But, axios' implementation whichs bases on XHR will remove all the body of GET request before sending.
Fetch api do the same thing like axios.

how to pass authtoken via header using angular js

I am trying to pass my api authtoken via the header. I am new to angular js so i am not able to do that. My code:
$scope.init=function(authtoken,cityname){
$scope.authtoken=authtoken;
$scope.cityname=cityname;
$http({method: 'GET', url: '/api/v1/asas?city='+$scope.cityname+'&auth='+$scope.authtoken}).success(function(data) {
Right now I am passing the authtoken in the api url. But I want to pass the token via the header.
usually you pass auth token in headers. Here is how i did it for one of my apps
angular.module('app', []).run(function($http) {
$http.defaults.headers.common.Authorization = token;
});
this will add auth token to headers by default so that you wont have to include is every time you make a request. If you want to include it in every call then it will be something like this
$http({
method: 'GET',
url: '/api/v1/asas?city='+$scope.cityname,
headers:{
'Authorization': $scope.authtoken
}
}).success(function(data) {
//success response.
}).error(function(error){
//failed response.
});
You can configure on application run
youapp.run(function($http) {
$http.defaults.headers.common.Authorization = 'Basic YmVlcDpib29w'
});
or pass it throw each request
$http({
url:'url',
headers:{
Authorization : 'Basic YmVlcDpib29w'
}
})
Angular $Http reference

Categories