如何在标头中传递参数

时间:2019-05-30 04:07:40

标签: node.js angular

我需要从我的Angular Node应用程序中调用一个API。为此,我必须传递用户名和密码以及标头请求。如何传递参数? 我尝试了以下代码

从Angular服务中,

checkUserApi(url: string): Observable<any> {    
   const headers = new HttpHeaders()
    .set('Authorization', 'my-auth-token')
    .set('Content-Type', 'application/json')
    .set('userName','prismtest')
    .set('password','12345678');
   return this.http.post('/upload/checkUserApi/', { headers })
  .map((response: Response) => {
    console.log(response);
   return response;
   })
}

在节点中,

router.post('/checkUserApi',function(req,res,next){
var proxyUrl = 'http://cors-anywhere.herokuapp.com/';

Request.post({
  "headers": { "userName": "prismtest","password":"12345678" },
  "url": proxyUrl+"https://example.com",
  "body": {}
  }, (error, response, body) => {  

  if(error) {
   return console.log(error);
  }  
   res.send(response);
  });
  });

4 个答案:

答案 0 :(得分:0)

您可以将表单参数作为第二个属性来传递请求:

checkUserApi(url: string): Observable<any> {    
   const headers = new HttpHeaders()
      .set('Authorization', 'my-auth-token')
      .set('Content-Type', 'application/json');

      return this.http.post('/upload/checkUserApi/', {username: 'prismtest', password: '123'}, { headers })
         .map((response: Response) => {
           console.log(response);
           return response;
          })
 }

答案 1 :(得分:0)

您可以使用Angular Service拦截器来做到这一点。 这应该是有帮助的:How to pass a param to HttpInterceptor?

答案 2 :(得分:0)

您可以尝试以下代码吗

/* SERVICE ANGULAR*/
import {Injectable} from '@angular/core';
import {HttpClient, HttpHeaders} from '@angular/common/http';
import {Observable,of} from 'rxjs';
import { map,catchError } from 'rxjs/operators';
import {User} from '../_models';

const httpOptions = {
    headers:new HttpHeaders({'Content-Type':'application/json'})
};

const API_URL = "http://localhost/app/api";
@Injectable({
    providedIn:'root',
})

export class UserService{
    constructor(private http:HttpClient){}

    /**
     * POST LOGIN USER
     */
    login(user: any) {
        return this.http.post<any>(API_URL+"/login", user,httpOptions)
            .pipe(map(user => {
                return user;
            }));
    } 
}

/*NODEJS*/
/* 1) install $ npm install body-parser --save*/
var bodyParser = require('body-parser');
app.use(bodyParser.json()); // support json encoded bodies
app.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies */
/ POST http://localhost:8080/api/users
// parameters sent with 
app.post('/api/login', function(req, res) {
    var user_id = req.body.id;
    var token = req.body.token;
    var geo = req.body.geo;

    res.send(user_id + ' ' + token + ' ' + geo);
});

您看到了:Use ExpressJS to Get URL and POST Parameters

Login State Angular + Laravel

答案 3 :(得分:0)

const headerOptions = {
     headers : new HttpHeaders({
       'Constent-Type' : 'application/json',
       'Authorization : 'you token here'
     })
   }

http.post('you action', {body Params}, headerOptions, function(res){
console.log('your respose is ', res);
})