标签: httpresponse

SqlFileStream:在 HTTP 响应中返回流与字节数组

我对使用 .net Web API 在 HTTP 响应中返回字节数组与流的问题有点困惑。

我遇到了以下代码:

        SqlConnection conn = new SqlConnection();
        SqlCommand cmd = conn.CreateCommand();
        cmd.CommandText = "Select FileData.PathName() As FilePath, GET_FILESTREAM_TRANSACTION_CONTEXT() AS Context From FileStorage";
        conn.Open();
        SqlDataReader reader = cmd.ExecuteReader();
        reader.Read();
        string filePath = (string)reader["FilePath"];

        byte[] fileBytes = (byte[])reader["Context"];
        SqlFileStream stream = new SqlFileStream(filePath, fileBytes, FileAccess.Read);

        result.Content = new StreamContent(stream);
        result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
        result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
Run Code Online (Sandbox Code Playgroud)

问题 1: 为什么他们会在 HTTP 响应中返回 Stream 而不是字节数组?

问题2:如果通过调用已经可以使用字节数组 ,为什么还要创建SqlFileStream来读取数据(byte[])reader["Context"]?这不是意味着整个文件内容都被读入内存吗?那么为什么需要 Stream 呢?

arrays httpresponse filestream asp.net-web-api asp.net-web-api2

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

使用自定义错误页面时删除 IIS 服务器标头

好的...我需要从我的生产服务器中删除“Microsoft:IIS8.5”服务器标头,我已经使用 URLReWrite 模块完成了该操作。这是我的规则:

<outboundRules>
    <rule name="replace server name header" patternSyntax="Wildcard">
        <match serverVariable="RESPONSE_Server" pattern="*" />
        <action type="Rewrite" value="MyServer" />
    </rule>
</outboundRules>
Run Code Online (Sandbox Code Playgroud)

这在所有常规页面上都非常有效。问题是我有自定义错误页面,因此当我遇到错误时,它会执行我的自定义错误(使用 MVC)并且看起来很棒。

但是,当它返回此自定义错误页面时,服务器变量又回到“Microsoft:IIS 8.5”

不知道为什么会这样做,因为 UrlReWrite 应该替换所有响应上的变量。

有什么想法为什么会发生这种情况吗?

iis httpresponse http-headers

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

Nginx proxy_pass 缺少错误主体

下面是一个非常标准的 nginx proxy_pass 设置:

server {
  listen 80;
  server_name ireport.jungdigital.com;
  access_log /var/log/nginx/ireport.access.log;
  root /var/www/ireport.jungdigital.com/dist;
  index index.html index.htm;
  location / {       
  }
  location /api/ {
    if ($request_method = 'OPTIONS') {
        add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Allow-Credentials' 'true';
        add_header 'Access-Control-Allow-Methods' 'GET, PUT, POST, OPTIONS';
        add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Reques
        add_header 'Access-Control-Max-Age' 1728000;
        add_header 'Content-Type' 'text/plain charset=UTF-8';
        add_header 'Content-Length' 0;
        return 204;
     }
     if ($request_method = 'POST') {
        add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Allow-Credentials' 'true';
        add_header 'Access-Control-Allow-Methods' 'GET, PUT, POST, OPTIONS';
        add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Reques
     }
     if ($request_method …
Run Code Online (Sandbox Code Playgroud)

error-handling nginx httpresponse http-status-code-404

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

Spring MV 3.2 异常响应映射

Spring 3.2.15,这里基于 MVC 的 REST API(不是Spring Boot,遗憾的是!)。我正在尝试实现一个满足以下条件的异常映射器/处理程序:

  • 无论发生什么(成功或错误),Spring 应用程序始终返回一个响应实体MyAppResponse(见下文);和
  • 如果成功处理请求,则返回 HTTP 状态 200(典型);和
  • 如果处理请求并发生异常,我需要控制特定异常到特定 HTTP 状态代码的映射
    • Spring MVC 框架错误(例如BlahException)必须映射到 HTTP 422
    • 自定义应用程序例外,例如我FizzBuzzException有自己的状态映射方案:
      • FizzBuzzException-> HTTP 401
      • FooBarException-> HTTP 403
      • OmgException-> HTTP 404
    • 所有其他异常,即非 Spring 异常和非自定义应用程序异常(上面列出的 3 个),应该生成 HTTP 500

对象所在位置MyAppResponse:

// Groovy pseudo-code
@Canonical
class MyAppResponse {
    String detail
    String randomNumber
}
Run Code Online (Sandbox Code Playgroud)

看起来也许能ResponseEntityExceptionHandler为我做到这一点,但我并没有透过树木看到森林,因为它是如何传递参数的。我希望我能做这样的事情:

// Groovy-pseudo code
@ControllerAdvice
class MyAppExceptionMapper extends ResponseEntityExceptionHandler {
    ResponseEntity<Object> handleFizzBuzzException(FizzBuzzException …
Run Code Online (Sandbox Code Playgroud)

exception spring-mvc httpresponse

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

发送 HttpResponse(status=200) 而不“返回” - 发送到哪里?

我正在使用 Slack Events API 来记录与我的 slackbot 的对话。

Events API 使用 HTTP POST 将消息推送到我的 Django 服务器。Slack 期望在 3 秒内收到 HTTP 200 响应,否则他们将重新 POST 消息。

我想存储/处理事件,但我不确定处理是否会在 3 秒内完成。我想将 HTTP Status 200 发送回 Slack,然后继续该过程,但我不知道将其发送到哪里。通常,我return HttpResponse(status=200)和 python 管理目的地/发送。

如何识别响应 URI?我可以发送HTTP Status 200吗urllib2?

import json
import urllib2
from django.http import HttpResponse

def processEvent(event):
    # do some stuff that takes longer than 3 seconds
    return

def incomingEvent(request):
    event = json.loads(request.body)
    url = request.?????
    req = urllib2.Request(url, HttpResponse(200))
    processEvent(event)
Run Code Online (Sandbox Code Playgroud)

python django httpresponse slack-api

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

System.Web.Http.Results.OkResult 与内容(HttpStatusCode.OK)

在 Web Api 2 中,我可以通过两种方式返回 200。

return Ok()或者return Ok("Some value")

或者

return Content(HttpStatusCode.OK, "Some value")它可以包含任何值,以及格式化程序和一些其他参数。

我知道return Content(...)支持所有内容HttpStatusCodes,因此它将用于响应,例如NoContent没有的地方return NoContent()

只是return Ok() 一个捷径吗return Content(..)?

c# httpresponse asp.net-web-api asp.net-web-api2

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

cakephp 3 中的 JSON 异常

我正在 cakephp 中做一个 Restfull api...有时我会抛出一些异常。例如:

if (!$this->request->is('post')) {
            throw new MethodNotAllowedException("The requested resource does not support http method " . $this->request->param('_method'));
        }
Run Code Online (Sandbox Code Playgroud)

我的问题是,当 url 为 /controller/action.json 时,响应为:

{
message: "The requested resource does not support http method GET",
url: "/api/auth/users/authenticate.json",
code: 405
}
Run Code Online (Sandbox Code Playgroud)

采用 json 格式,但是,当我的 url 是 /controller/action 时。我的响应是 HTML,我想知道是否可以强制这些异常始终为 json,而不将 .json 放入 url 中。

谢谢!

json exception httpresponse cakephp-3.0

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

RESOURCE NOT FOUND (404) 何时合适?

我一直在与同事进行长时间的讨论,讨论针对 url 的不同错误部分是否使用 404 或 400(错误请求)进行响应。

假设我们有一个提供产品的端点。但产品是细分的,并且取决于国家、渠道(如 B2B 或 B2C)等。

在像这样的网址中

/myapi/v1/{国家}/{渠道}/产品/{产品 ID}

如果呼叫者给了我一个错误的“产品 ID”,我会很清楚地回复 RESOURCE NOT FOUND。

但“国家”呢?我认为“国家/地区”不是当前 url 提供的资源,因此响应(在提供的“国家/地区”不好的情况下)应该是 BAD REQUEST。其他人认为整个 url 就像资源的签名,错误的“国家/地区”将导致无法返回 200,因此 RESOURCE NOT FOUND 应该是响应状态。

哪一种状态是正确的?什么是正确的问题?

rest url httpresponse

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

PHP 7 发送响应后继续执行

我正在尝试从函数向客户端发送响应数据并继续执行。我按照下面的代码

ignore_user_abort(true);
set_time_limit(0);

ob_start();
// do initial processing here
echo $response; // send the response
header('Connection: close');
header('Content-Length: '.ob_get_length());
ob_end_flush();
ob_flush();
flush();
// check if fastcgi_finish_request is callable
if (is_callable('fastcgi_finish_request')) {
    fastcgi_finish_request();
}
Run Code Online (Sandbox Code Playgroud)

从问题中得到代码

发送http响应后继续处理php

我得到的只是 200 ok 响应,而不是我回显的数据。

我也需要获取响应数据。我使用的是php7.1。php5和7的使用有什么区别吗?

请帮忙

php fastcgi httpresponse

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

Segment.io HTTP API 不收集事件

这个特定Segment.io的文档和帮助是有限且稀疏的,所以我希望可以在这里询问。

我刚刚设置了一个 Segment.io 工作区和一个 HTTP API 源 HTTP API 源在 Segment.io UI 中可见

根据文档,我向https://api.segment.io/v1/track和https://api.segment.io/v1/page端点发送了一些 POST 请求(使用 Postman)。请求的结构如下:

curl -X POST \
  https://api.segment.io/v1/track \
  -H 'Accept: */*' \
  -H 'Authorization: My4w3s0m3k3y' \
  -H 'Cache-Control: no-cache' \
  -H 'Connection: keep-alive' \
  -H 'Content-Type: application/json' \
  -H 'Host: api.segment.io' \
  -H 'Postman-Token: 474d7fbe-15af-43d2-b629-61e15945e662,2c3d5fbe-2c09-4fe6-b7ea-a04e3221201b' \
  -H 'User-Agent: PostmanRuntime/7.11.0' \
  -H 'accept-encoding: gzip, deflate' \
  -H 'cache-control: no-cache' \
  -H 'content-length: 117' \
  -d '{
  "userId": "abc123",
  "event": "My tests",
  "properties": {
    "name": "test …
Run Code Online (Sandbox Code Playgroud)

httpresponse httprequest segment-io httpapi

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