我对使用 .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
好的...我需要从我的生产服务器中删除“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 应该替换所有响应上的变量。
有什么想法为什么会发生这种情况吗?
下面是一个非常标准的 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) Spring 3.2.15,这里基于 MVC 的 REST API(不是Spring Boot,遗憾的是!)。我正在尝试实现一个满足以下条件的异常映射器/处理程序:
MyAppResponse(见下文);和BlahException)必须映射到 HTTP 422FizzBuzzException有自己的状态映射方案:
FizzBuzzException-> HTTP 401FooBarException-> HTTP 403OmgException-> HTTP 404对象所在位置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) 我正在使用 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) 在 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(..)?
我正在 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 中。
谢谢!
我一直在与同事进行长时间的讨论,讨论针对 url 的不同错误部分是否使用 404 或 400(错误请求)进行响应。
假设我们有一个提供产品的端点。但产品是细分的,并且取决于国家、渠道(如 B2B 或 B2C)等。
在像这样的网址中
/myapi/v1/{国家}/{渠道}/产品/{产品 ID}
如果呼叫者给了我一个错误的“产品 ID”,我会很清楚地回复 RESOURCE NOT FOUND。
但“国家”呢?我认为“国家/地区”不是当前 url 提供的资源,因此响应(在提供的“国家/地区”不好的情况下)应该是 BAD REQUEST。其他人认为整个 url 就像资源的签名,错误的“国家/地区”将导致无法返回 200,因此 RESOURCE NOT FOUND 应该是响应状态。
哪一种状态是正确的?什么是正确的问题?
我正在尝试从函数向客户端发送响应数据并继续执行。我按照下面的代码
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)
从问题中得到代码
我得到的只是 200 ok 响应,而不是我回显的数据。
我也需要获取响应数据。我使用的是php7.1。php5和7的使用有什么区别吗?
请帮忙
这个特定Segment.io的文档和帮助是有限且稀疏的,所以我希望可以在这里询问。
我刚刚设置了一个 Segment.io 工作区和一个 HTTP API 源

根据文档,我向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 ×10
exception ×2
arrays ×1
c# ×1
cakephp-3.0 ×1
django ×1
fastcgi ×1
filestream ×1
http-headers ×1
httpapi ×1
httprequest ×1
iis ×1
json ×1
nginx ×1
php ×1
python ×1
rest ×1
segment-io ×1
slack-api ×1
spring-mvc ×1
url ×1