如何从localhost(http)发送POST请求到django(https)?

时间:2018-03-16 11:25:27

标签: django http https proxy django-csrf

发送给代理/api,所有参数(标题/ Cookie /帖子)为docs

PrtScreen with request param (header/cookie/post)

得到

PrtScreen with response 403 Forbidden

server.js

'use strict';

const fs = require('fs'),
    proxy = require('http-proxy-middleware'),
    browserSync = require('browser-sync').create();

function returnIndexPageFn(req, res, next) {
    res.writeHead(200, {'Content-Type': 'text/html'});
    res.write(fs.readFileSync('./public/app.html'));
    res.end();
    next();
}

browserSync.init({
    port: 88,
    server: {
        baseDir: 'public/',
        index: 'app.html',
        middleware: [
            {route: '/home', handle: returnIndexPageFn},
            proxy(['/api', '/media'], {
                target: 'https://security-site.com',
                logLevel: 'debug',
                changeOrigin: true,
                headers: {
                    Referer: 'https://security-site.com',
                },
            })
        ]
    }
});

我用角度5尝试另一个,但结果相同((

proxy.conf.json

{
    "/api": {
      "target": "https://security-site.com/",
      "secure": false,
      "changeOrigin": true,
      "logLevel": "info"
    }
}

如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

我找到解决方案:

需要将标题Referal更改为 https 协议

browser-sync

<强> server.js

...
    proxy(['/api', '/media'], {
        target: 'https://security-site.com',
        logLevel: 'debug',
        changeOrigin: true,
        headers: {
            Referer: 'https://security-site.com',
        },
    })
...

对于角度5(角度cli): 的 proxy.conf.json

{
    "/api": {
        "target": "https://security-site.com/",
        "headers": {
            "Referer": "https://security-site.com/"
        },
        "secure": false,
        "changeOrigin": true,
        "logLevel": "info"
    }
}