简体   繁体   English

如何在同步请求中使用承载令牌为POST方法授权?

[英]How to use bearer token for authorization for POST method in sync-request?

How can we use bearer token with POST method using npm sync-request? 我们如何通过npm sync-request将承载令牌与POST方法一起使用? The sync-request resource page has the way to use authorization in GET request but not in POST request. 同步请求资源页面可以在GET请求中使用授权,而在POST请求中不能使用授权。

*******GET Request*******
var request = require('sync-request');
var res = request('GET', 'https://example.com', {
'headers': {
'user-agent': 'example-user-agent'
}
});

****POST Request*****
var request = require('sync-request');
var res = request('POST', 'https://example.com/create-user', {
  json: { username: 'Name' }
});

Not sure why you would want to use sync-request which can cause timing issues but this should work with either sync-request or request 不知道为什么要使用可能导致时序问题的sync-request ,但这应该与sync-requestrequest

 // *******GET Request******* var request = require('sync-request'); var res = request('GET', 'https://example.com', { 'headers': { 'user-agent': 'example-user-agent', 'authorization', 'Bearer ' + authId } }); // ****POST Request***** var request = require('sync-request'); var res = request('POST', 'https://example.com/create-user', { 'headers': { 'authorization', 'Bearer ' + authId }, json: { username: 'Name' } }); 

authId needs to be whatever your bearer token spoils be for your app. authId必须是您的应用对承载令牌的破坏。

I would suggest use of axis and example below:- GET 我建议在下面使用轴和示例: -GET

import axios from "axios"; 从“ axios”导入axios;

 axios({
        method: 'get',
        url: url,
        headers: {
            'Content-Type': 'application/json'
        }
    }).then(function (response) {
      console.log(response);
    }).catch((err) => {
       console.log(err)
      ));

POST 开机自检

             axios({
                    method: 'post',
                    url: url,
                    data: JSON.stringify({orders}),
                    headers: {
                        'Content-Type': 'application/json',
                        'Authorization': userObj.token
                    }
                }).then(function (response) {
                     console.log(response)
                });

Where ubserObj.token - 哪里ubserObj.token-

Bearer Token ex: Bearer ASDF@!@#!ADFASDF!@#!@# 例如Bearer代币: Bearer ASDF @!@#!ADFASDF!@#!@#

This will be on the server side settings. 这将在服务器端设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM