I am using Angular 2 as front end. I tried to send an Object { test: 'Hi' }.
When my http header is like this:
let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({ headers: headers });
I can get the content I sent on the server side using req.body.
However, when my http header is like this:
let headers = new Headers({ 'Authorization': 'Bearer ' + token });
let options = new RequestOptions({ headers: headers });
When I use req.body again, I got an empty Object {}.
My server is using Express.js, and my bodyParser is like this:
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
How can I do it correctly? Thanks
You should send both headers to express:
let headers = new Headers({
'Content-Type': 'application/json',
'Authorization': 'Bearer ....'
});
Related
Here is my code:
var url = 'http://myURL';
String tokens = "good token working on postman";
var response = await http.get(url, headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': 'Bearer $tokens',
});
print('Token : ${tokens}');
print(response.body);
The problem is that the token and the api url is working well on PostMan but it looks like flutter doesn't send the token.
It returns me a web page content.
var response = await http
.get(Uri.parse('http://192.168.1.32:8000/api/users'), headers: {
'Authorization': 'Bearer ' + sharedPreferences.getString("access_token"),
});
I'm trying to get data from my local database.
local url: http://localhost:8000/
I call this in my service /getPersonalInfoData
status showing 200, but I can't see any data there.
component.ts:
var data = this.personalInfoService.getPersonalInfoData()
.subscribe(arg => this.driverData = arg);
console.log(data);
personalInfoService:
return this.http.get('/getPersonalInfoData').map((res: Response) => { console.log(res); return res; });
backend data source API(Nodejs):
routes.get('/getPersonalInfoData',personal_info_controller.getPersonalInfoData);
In personalInfoService:-
1.import { Http, Response, Headers, RequestOptions, URLSearchParams} from'#angular/http';
2.In method add this code:
const headers = new Headers({
'Content-Type': 'application/json',
'Cache-control': 'no-cache',
Expires: '0',
Pragma: 'no-cache'
});
const options = new RequestOptions({ headers: headers });
return this.http.get(Url, options).map(res => {
return res.json();
});
Hope this will help you.
I am working with authentication using Angular and .Net Web API 2 back end. My registration route, and other resources are working, however the login/token is not.
In postman, this request works and I get the token back:
In angular my code looks like the following:
credentials.grant_type = "password";
credentials.userName = "email#email.com";
credentials.password = "asdfasdf";
let headers = new Headers({ 'Content-Type': 'application/x-www-form-urlenconded' });
let options = new RequestOptions({ headers: headers });
return this.http.post('http://localhost:58352/Token', credentials, options).map((response: Response) => {
return response.json();
});
However, I get the response:
{"error":"unsupported_grant_type"}
In Angular.js (or Angular 1) I used transformRequest to get it working.
This did the trick!
let urlSearchParams = new URLSearchParams();
urlSearchParams.append('userName', 'email#email.com');
urlSearchParams.append('password', 'asdfasdf');
urlSearchParams.append('grant_type', 'password');
let body = urlSearchParams.toString()
let headers = new Headers({ 'Content-Type': 'application/x-www-form-urlenconded' });
let options = new RequestOptions({ headers: headers });
return this.http.post('http://localhost:58352/Token', body, options).map((response: Response) => {
return response.json();
});
I am trying to post data to my server, but I get a 400 Error when executing. The same request works when using get. (the post request is used for a post function and I tried the get request with a get function so that is not an issue)
This is my code:
var json =
{
"test":"asdf"
}
let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({ headers: headers });
this.http.post("http://192.168.0.103:3000/testPost",json,headers)
.subscribe(data => {
console.log(data);
},
data =>{
console.log(data);
});
Im doing this:
let headers = new Headers({
'Content-Type': 'application/json',
'Content-Encoding': 'deflate'
});
let options = new RequestOptions({ headers: headers });
let body = JSON.stringify({
"password": password,
"username": username
});
return this.http.post('URL HERE', body, headers).map((res: Response) => res.text());
but it returns something like this:
��V���0���C�`Y�F��Z��J��2�%n�l�f[��k�- I�f�M}�F̘y3o<�������J��-SYV�]�#i�,[���T���3�?�/fv��V$��ʈl�Ŀ��2?<�'B5� V��`��&kA !�FC�VJ�㍻�,/�+�fKw�S���%�Ѓ|�QJ>}�9�7fz���9zL�M�Gt�!�#?:�6��?�fH�k̄���Q(����z�7��U�r2�7��g2�[�
When I do the call from Postman, no issues.
Any tips ?
this.http.post('URL HERE', body, headers) should have been this.http.post('URL HERE', body, options)
stupidme