标签: http-post

如何从简单的 Ruby 服务器捕获 POST 数据

我有一个基本的 Ruby 服务器,我想侦听特定端口,读取传入的 POST 数据并执行等等...

我有这个:

require 'socket'               # Get sockets from stdlib

server = TCPServer.open(2000)  # Socket to listen on port 2000
loop {                         # Servers run forever
  client = server.accept       # Wait for a client to connect
  client.puts(Time.now.ctime)  # Send the time to the client
  client.puts "Closing the connection. Bye!"
  client.close                 # Disconnect from the client
}
Run Code Online (Sandbox Code Playgroud)

我将如何捕获 POST 数据?

谢谢你的帮助。

ruby http-post

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

PHP - HTTP GET|PUT|POST 的原生常量

是否有在原生 php 语言中定义的“GET”、“POST”等常量,我已经尝试过:

HTTP_METH_GET
Run Code Online (Sandbox Code Playgroud)

等,
但没有成功

php constants http-get http-post httprequest

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

将 POST 变量添加到 Go test http 请求

我正在尝试将表单变量添加到 Go http 请求中。

这是我的 Go 测试的样子:

func sample_test(t *testing.T) {
    handler := &my_listener_class{}
    reader := strings.NewReader("number=2")
    req, _ := http.NewRequest("POST", "/my_url", reader)
    w := httptest.NewRecorder()
    handler.function_to_test(w, req)
    if w.Code != http.StatusOK {
        t.Errorf("Home page didn't return %v", http.StatusOK)
    }
}
Run Code Online (Sandbox Code Playgroud)

问题是表单数据永远不会传递给我需要测试的函数。

另一个相关函数是:

func (listener *my_listener_class) function_to_test(writer http.ResponseWriter, request *http.Request) {
    ...
}
Run Code Online (Sandbox Code Playgroud)

我正在使用 Go 版本 go1.3.3 darwin/amd64。

testing unit-testing http-post go

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

取消“HttpClient”POST 请求

我正在使用HttpClient.PostAsync()我的 Windows Phone 8 应用上传图像。用户可以选择通过 UI 按钮取消此上传。

为了取消 POST 请求,我设置了一个CancellationToken. 但这不起作用。在取消请求之后,我仍然看到在我的代理中进行了上传,很明显该请求被忽略了。我的代码:

using (var content = new MultipartFormDataContent())
{
    var file = new StreamContent(stream);
    file .Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
    {
        FileName =  "filename.jpg",
    };
    file.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg");
    content.Add(file);

    await httpclient.PostAsync(new Uri("myurl", UriKind.Absolute), content,
        cancellationToken);
}
Run Code Online (Sandbox Code Playgroud)

另请注意,我有一CancellationTokenSource对CancellationToken。在用户点击取消按钮后,tokensource.Cancel()被调用。此外,我的测试用例中的图像从 1 到 2 MB(不是那么大)。

那么,有没有办法取消HttpClientPOST 请求?

c# httpclient http-post cancellation windows-phone-8

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

在 Postman 中渲染 HTML 响应

我正在使用 Postman v4.9.3,我正在尝试使用 POST 请求查看页面响应。问题是,当单击“预览”按钮时,未加载外部 CSS。有人可以帮我解决吗?或者是否有其他方法使用 POST 请求页面并设置一些标头,然后渲染返回的页面?

在此输入图像描述

html http-post postman

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

我可以创建多个具有相同请求映射和不同请求正文的 HTTP POST 方法吗

我可以创建多个具有相同请求映射和不同请求正文的 HTTP POST 方法吗

@RequestMapping("api/v1/")
@RequestMapping(value = "test" ,method = RequestMethod.POST)
public RObjet create(@RequestBody RBody rbody)
{  
 // do some process...  
} 

@RequestMapping("api/v1/")
@RequestMapping(value = "test" ,method = RequestMethod.POST)
public RObjet2 create2(@RequestBody RBody2 rbody)
{  

  // do something.  

} 
Run Code Online (Sandbox Code Playgroud)

这可能吗。?我们如何在 Spring Boot 中处理这个问题。

java http-post spring-boot

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

是否可以在 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
查看次数