标签: http-post

是否可以在 XMLHttpRequest 标头中包含 antiforgerytoken?

我正在使用一个名为Slim 的库将图像上传到我的 asp.net MVC 项目。

JQuery 库使用 XMLHttpRequest 向服务器发送请求,我已经阅读了所有手册,但没有任何关于如何将防伪造令牌包含到服务器的内容。

所以我在发送之前手动添加了这一行:

data.__RequestVerificationToken = $('input[name="__RequestVerificationToken"]').val();
Run Code Online (Sandbox Code Playgroud)

即使令牌包含在数据中,Asp.net MVC 过滤器:ValidateJsonAntiForgeryTokenAttribute也无法识别它,因为它期望令牌位于 header中。这是服务器上的验证代码:

AntiForgery.Validate(cookie != null ? cookie.Value : null,
                             httpContext.Request.Headers["__RequestVerificationToken"]);
Run Code Online (Sandbox Code Playgroud)

这是库的发送函数,其中包含我在底部添加的额外行以包含令牌。如何将令牌包含在标头中?

var send = function send(url, data, requestDecorator, progress, success, err) {

var xhr = new XMLHttpRequest();

// if progress callback defined handle progress events
if (progress) {
    xhr.upload.addEventListener('progress', function (e) {
        progress(e.loaded, e.total);
    });
}

// open the request
xhr.open('POST', url, true);

// if request decorator defined pass …
Run Code Online (Sandbox Code Playgroud)

jquery xmlhttprequest http-post asp.net-mvc-5

5
推荐指数
1
解决办法
2654
查看次数

“GetAddrInfoReqWrap.onlookup [未完成] (dns.js:76:26)”

我正在尝试使用请求库运行 lambda 函数。我尝试查看其他一些相同的问题,但找不到我需要的问题。以下是我的代码:

var req = require('request');

var headers = { 'X-AUTH-TOKEN': 'xxxx',
                'Content-Type': 'application/json',
                'Accept': 'application/json'};

var body = {"name" : "omkarEmailTest", "command_type":"SparkCommand", "language":"notebook", "note_id":"56563", "label":"YetAnotherSparkCluster"};

// Configure the request
var options = {
    url: 'http://api.quobole.com/api/v1.2/commands',
    method: 'POST',
    headers: headers,
    body: body,
    json: true
};

exports.handler = (event, context, callback) => {
 req(options, function (error, response, body) {
    console.log(body);
    callback(error);
 });
}
Run Code Online (Sandbox Code Playgroud)

以下是我在 aws lambda 控制台中收到的日志:

Response:
{
  "errorMessage": "getaddrinfo ENOTFOUND api.quobole.com api.quobole.com:80",
  "errorType": "Error",
  "stackTrace": [
    "errnoException (dns.js:28:10)", …
Run Code Online (Sandbox Code Playgroud)

http-post node.js crossdomain-request.js aws-lambda aws-sdk-nodejs

5
推荐指数
0
解决办法
2640
查看次数

Request.Content.IsMimeMultipartContent() 失败

我正在尝试使用 Html2 输入 type="file" 和 angular2 http.post 请求上传文件。当请求到达 web api 时,它失败了

Request.Content.IsMimeMultipartContent()
Run Code Online (Sandbox Code Playgroud)

使用Postman提交请求时不会失败(当我不在标头中包含 Content-Type 时,因为邮递员会处理它)。

请参阅我的代码:Html:

 <input type="file" (change)="fileChange($event)" placeholder="Upload file" accept=".pdf,.doc,.docx,.dwg,.jpeg,.jpg">
Run Code Online (Sandbox Code Playgroud)

服务功能:

uploadFile(event) {
    let fileUploadUrl = this.webApiFileUploadURL;
    let fileList: FileList = event.target.files;
    if(fileList.length > 0) {
        let file: File = fileList[0];
        let formData:FormData = new FormData();
        formData.append('uploadFile', file, file.name);
        let headers = new Headers();
        headers.append('Content-Type', 'multipart/form-data');
        headers.append('Accept', 'application/json');
        let options = new RequestOptions({ headers: headers });
        this._http.post(`${this.webApiFileUploadURL}`, formData, options)
            .map(res => res.json())
            .catch(error => Observable.throw(error)) …
Run Code Online (Sandbox Code Playgroud)

c# http-post asp.net-web-api

5
推荐指数
2
解决办法
8838
查看次数

POST 响应缓存在 nginx 中不起作用

我的任务是使用nginx实现微缓存策略,即缓存一些POST端点的响应几秒钟。

在http部分nginx.conf我有以下内容:

proxy_cache_path /tmp/cache keys_zone=cache:10m levels=1:2 inactive=600s max_size=100m;
Run Code Online (Sandbox Code Playgroud)

然后我location有server:

    location /my-url/ {
      root dir;
      client_max_body_size 50k;
      proxy_cache cache;
      proxy_cache_valid 10s;
      proxy_cache_methods POST;
      proxy_cache_key "$request_uri|$request_body";
      proxy_ignore_headers Vary;

      add_header X-Cached $upstream_cache_status;

      proxy_pass http://my-upstream;
    }
Run Code Online (Sandbox Code Playgroud)

该应用程序位于输出处,如果我理解正确的my-upstream话Cache-Control: max-age=10,应该使响应可缓存。

但是当我在短时间内(不到10秒)使用curl发出重复请求时

curl -v --data "a=b&c=d" https://my-host/my-url/1573
Run Code Online (Sandbox Code Playgroud)

它们全部到达后端(根据后端日志)。还有,X-Cached总是MISS。

请求和响应如下:

> POST /my-url/1573 HTTP/1.1
> Host: my-host
> User-Agent: curl/7.47.0
> Accept: */*
> Content-Length: 113
> Content-Type: application/x-www-form-urlencoded
> 
* upload …
Run Code Online (Sandbox Code Playgroud)

caching nginx http-post nginx-location nginx-cache

5
推荐指数
1
解决办法
2561
查看次数

为什么通过 Python“requests”发出的请求比在 Postman 中花费的时间几乎是七倍?

我目前正在 Python 2.7 中向服务器发出 HTTP Post 请求,requests.post()大约需要 700 毫秒。也绝对没有可能导致延迟的代理服务器,但我仍然绕过任何代理,因为这似乎是该库的问题。

\n\n

不过我对那一次很好奇,因为在我看来,大约230个字符的答案需要很长时间。这就是为什么我在 Postman 中尝试了相同的请求。结果是Postman中的请求花费了不到100ms!这更适合我要做的任务。一切都与时间有关。

\n\n

我想知道是否有任何特定参数requests.post()需要设置,或者这个功能是否那么慢?

\n\n

目前的请求看起来像这样(非常基本的东西):

\n\n
req = requests.post(url, json={"Username": username, "Password": password, "TerminalNo": terminalno)}) \njson = req.json()\n
Run Code Online (Sandbox Code Playgroud)\n\n

如果需要,来自服务器的标头:

\n\n
cache-control \xe2\x86\x92private\ncontent-length \xe2\x86\x92228\ncontent-type \xe2\x86\x92application/json; charset=utf-8\ndate \xe2\x86\x92Mon, 30 Jul 2018 17:58:05 GMT\nserver \xe2\x86\x92Microsoft-IIS/7.5\nx-aspnet-version \xe2\x86\x922.0.50727\nx-powered-by \xe2\x86\x92ASP.NET\n
Run Code Online (Sandbox Code Playgroud)\n

python http-post python-requests

5
推荐指数
1
解决办法
3864
查看次数

如何在 Angular Typescript 中将数组作为表单数据发布

我正在 Angular 8 中工作,并在 .net core 2.2 中使用 web.api

我有一个表单,包括一些“常规”输入、一个文件和多个可选复选框。

问题是多选复选框,我将其转换为数字数组。在将数据发布到服务器之前,我将 FormGroup 转换为 FormData 并手动添加文件以及多选复选框中的整数数组:

data.append('locationsSecondaryIds',
    new Blob( [ JSON.stringify(this.profilePersonalData.locationsSecondaryIds)], { type : 'application/json' } ) );
    if (this.file) {
        data.append('document', this.file, this.file.name);
    }
Run Code Online (Sandbox Code Playgroud)

locationSecondaryIds 是一个数字[]。

我也尝试过忽略 Blob 并将其作为 [1,1] 发送,但是当服务器到达服务器时,它无法将其转换为列表

有什么好的建议吗?

首先十分感谢 :-)

arrays http-post typescript asp.net-core-webapi angular

5
推荐指数
2
解决办法
1万
查看次数

org.http4s.client 带有标题和 UriForm 的帖子

使用 org.http4s.client 无法找到如何将标头和 UriForm 与 Post 请求一起发送。

import org.http4s.client.dsl.io._
import org.http4s.Method._

val lstHeader: List[Header] = List(
  Header("Accept", "application/json")
  , Header("Accept-Charset", "utf-8")
  , Header("Accept-Encoding", "gzip")
)

val formData :UrlForm = UrlForm(
  "username" -> "user",
  "enc_password" -> "password",
  "queryParams" -> "{}",
  "optIntoOneTap" -> "false"
)

val req1 = POST(
  formData,
  uri"https://www.instagram.com/accounts/login/ajax/"
)

val req2: Request[IO] = Request[IO](
  Method.POST,
  uri"https://www.instagram.com/accounts/login/ajax/",
  HttpVersion.`HTTP/2.0`,
  Headers(lstHeader)
)
Run Code Online (Sandbox Code Playgroud)

req1 没有我的标题 req2 没有表单数据

谢谢

scala http-post http4s

5
推荐指数
1
解决办法
1555
查看次数

如何验证来自 Slack Events API 的请求

我正在使用validate-slack-request包来验证传入的 slack 请求是否来自 slack。这对于斜杠命令和交互组件(按钮等)来说效果很好。但是它不适用于事件 API

我注意到事件 API 的 POST 请求正文具有不同的格式。没有payload. 但我不清楚什么是松弛让我可以用来验证。代码如下

//This WORKS
app.post("/interactiveCommand", async (req, res) => {
  const legit = validateSlackRequest(process.env.SLACK_SIGNING_SECRET, req, false);
  if (!legit) {
    console.log("UNAUTHORIZED ACCESS ", req.headers, req.body);
    return res.status(403).send("Unauthorized");
  }
  await interactiveCommand(...);
  return;
});

//This does NOT WORK
app.post("/slackEvents", parser, json, async (req, res) => {
  const legit = validateSlackRequest(process.env.SLACK_SIGNING_SECRET, req, false);
  if (!legit) {
    console.log("UNAUTHORIZED ACCESS ", req.headers, req.body);
    res.status(403).send("Unauthorized");
  } else {
    try {
      switch (req.body.event.type) {
        case …
Run Code Online (Sandbox Code Playgroud)

http-post node.js express slack-api

5
推荐指数
1
解决办法
1782
查看次数

我们如何使用基于函数的视图在 django Rest 框架中发布数据?

我是 django Rest Framework (DRF) 的新手,我需要POST使用基于函数的视图 (FDV) 的一些数据。我成功地使用了GET这种方式的方法,但不知道如何使用 POST 方法将值添加到数据库。

# app/models.py

class Item(models.Model):
    name = models.CharField(max_length=50)
    quantity = models.IntegerField()
    price = models.FloatField()


# app/urls.py

urlpatterns = [
path('', views.get_data_list, name='list'),
path('post_val/', views.post_data, name='post_val'), # need to implement
]

# app/serializers.py

class ItemSerializer(serializers.ModelSerializer):
    class Meta:
        model = Item
        fields = ('id', 'name', 'quantity','price')



# app/views.py

from django.http.response import JsonResponse
from rest_framework.parsers import JSONParser 
 
from .models import Item
from .serializers import ItemSerializer
from rest_framework.decorators import api_view

@api_view(['GET',]) …
Run Code Online (Sandbox Code Playgroud)

django json http-post django-views django-rest-framework

5
推荐指数
1
解决办法
1万
查看次数

读取 React 应用程序请求中的 Post 正文

我在谷歌上搜索了 React 的 Post 请求,但我得到的只是 React 发送 Post 请求的示例。我需要能够读取发送到反应应用程序的发布请求。我有一个 Get 请求发送到 React 的示例,其中参数位于 this.props.location.search 中。我需要同样的东西,但对于邮政。

http-post reactjs

5
推荐指数
0
解决办法
1010
查看次数