我有一个显示供应商产品的 API
我们的 UI 将允许用户选择各种过滤条件,并使用它一次删除大量产品。
问题是执行几千个单独的 HTTP 删除请求需要太长时间:
DELETE /api/supplier/6/products/5
DELETE /api/supplier/6/products/7
DELETE /api/supplier/6/products/8
DELETE /api/supplier/6/products/10
...
Run Code Online (Sandbox Code Playgroud)
目的是进行一次 HTTP 调用以一次删除一堆供应商产品。我可以将一个正文传递给删除,以便它包含我们要删除的所有 ID 的列表:
DELETE /api/supplier/6/products
Body:
{
"DeleteIds": "[5,7,8,10]"
}
Run Code Online (Sandbox Code Playgroud)
这很有效,直到我们把它放在我们的生产代理防火墙后面,它从 DELETE 请求中剥离了主体。
我看过 HTTP Spec RFC 2616 Fielding 等。并且它没有明确说明我不应该在 DELETE 请求中使用正文,进一步阅读表明发送带有 DELETE 请求的正文没有任何问题。
我可以控制我们的代理服务器,并且能够允许所有请求都通过正文,但我担心这可能不是最佳做法。我们可能有数千个要通过的 Id,我不希望我们使用标头或 URL 参数,因为我们可能会遇到长度限制。
所以我的问题是:使用请求正文 对多个产品进行删除的正确方法是什么?不仅仅是一个意见,而且是否有实际记录的证据来说明为什么我不应该使用 HTTP DELETE 的正文?
我应该继续
DELETE /api/supplier/6/products (Using a body)
Run Code Online (Sandbox Code Playgroud)
或者不应该将 DELETE 与正文一起使用,而是对类似的内容进行 POST
POST /api/supplier/6/products/deletemultiple
Run Code Online (Sandbox Code Playgroud)
编辑:在这个问题中有一些很好的辩论:删除一堆项目的宁静方式 它没有解决我关于使用 DELETE 请求的正文进行自定义删除操作的问题,但是有一些关于不同方式的很好的辩论可能会发生批量删除。
假设我有:
@GET
public UserList fetch(@PathParam("user") String userId) {
// Do stuff here
}
Run Code Online (Sandbox Code Playgroud)
现在,假设我有自己的类型userId,我们称之为UserId。是否可以将其解析String为UserId将其传递到fetch方法中,即:
@GET
public UserList fetch(@PathParam("user") UserId userId) {
// Do stuff here
}
Run Code Online (Sandbox Code Playgroud)
我意识到一旦进入方法,我就可以解析字符串,但是我的方法获取我想要的类型会更方便。
我正在从Insomnia(类似于PostMan的软件)发送此请求。我正在发送一个简单的XML文件
<?xml version='1.0' encoding='UTF-8'?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
Run Code Online (Sandbox Code Playgroud)
并且在标头上,我只有Content-Type:application / xml 我将其发送到包含以下代码的php文件:
$xml = file_get_contents('php://input');
echo "<pre>";
print_r(htmlspecialchars($xml));
echo "</pre>";
Run Code Online (Sandbox Code Playgroud)
输出一个空字符串。在我的本地服务器上,它正在输出应有的xml文件。
输出来自以下代码:
echo "<pre>";
print_r($_POST);
echo "</pre>";
Run Code Online (Sandbox Code Playgroud)
输出空数组,并:
if (!empty($_SERVER['CONTENT_TYPE']) && $_SERVER['CONTENT_TYPE'] == 'application/xml') {
echo "application/xml";
}
Run Code Online (Sandbox Code Playgroud)
输出“ application / xml”。
答:在我看来,我应该用HTTPS而不是HTTP来写地址。
我正在尝试以角度提出此请求:
curl --location --request GET 'http://localhost:5000/location/searchDistanceAndTags?pageNo=1&size=1' \
--header 'token: $IKFASOmV#*+JIL4X0FGTDZNRPB3GN7HvAB9QD2X8QWeLsqtKW1U5YHnxCMRywEbUZlu??oz!!!!6r' \
--header 'Content-Type: application/json' \
--data-raw '{
"lat": "38.8993487",
"lng": "-77.0145665",
"radius": "100",
"tags": ["bier"]
}'
Run Code Online (Sandbox Code Playgroud)
这是我的角度代码,不幸的是参数没有设置(后端没有收到 req.body 中的任何数据)
const dataset = {
lat: '38.8993487',
lng: '-77.0145665',
radius: '100',
tags: ['bier']
};
return this._httpClient.get<any>(`http://localhost:5000/location/searchDistanceAndTags?pageNo=${this.pageNumber}&size=10`, {
headers: {token: this.apikey,
'Content-Type': 'application/json'},
params: dataset
});
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用request.post()Node.js中的请求包将大文件发布到另一台服务器,我得到:
(node) warning: Recursive process.nextTick detected. This will break in the next version of node. Please use setImmediate for recursive deferral.
RangeError: Maximum call stack size exceeded
Run Code Online (Sandbox Code Playgroud)
我认为这是因为它是一个大文件(80MB),因为较小的文件工作正常,所以我现在正在尝试实现一个流:
var options = {
url: aURLIHaveDefined,
headers: {
'User-Agent': 'MyRestAPI'
},
auth: {
username: creds.username,
password: creds.password
},
strictSSL: false,
json: true
}
var readStream = fs.createReadStream(filePathIHaveDefined);
readStream.pipe(options);
Run Code Online (Sandbox Code Playgroud)
我收到错误:
TypeError: Object #<Object> has no method 'on'
Run Code Online (Sandbox Code Playgroud)
有没有人知道在管道中包含选项的语法以及为什么我收到此错误?
更新(在sousa评论之后): 我试过:
readStream.pipe(request(options));
readStream.on('end', function() {
console.log('end of readStream'+fileName);
});
Run Code Online (Sandbox Code Playgroud)
它没有错误,但现在似乎也没有做任何事情.程序刚执行,我从未见过console.log()? …
我一直在尝试向FormRequest我的删除方法添加规则和消息,但请求将返回空白,并且规则每次都失败.
是否可以在删除方法中获取请求数据?
这是我的请求类:
use App\Http\Requests\Request;
class DeleteRequest extends Request
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'staff_id' => ['required', 'exists:users,uid'],
'reason' => ['required', 'string'],
];
}
/**
* Get custom messages for validator errors.
*
* @return array …Run Code Online (Sandbox Code Playgroud) 我正在尝试对弹性搜索 api 执行 GET 请求,这是这种形式所需要的
GET /_search
{
"query": {
"more_like_this" : {
"fields" : ["title", "description"],
"like" : "Once upon a time",
"min_term_freq" : 1,
"max_query_terms" : 12
}
}
}
Run Code Online (Sandbox Code Playgroud)
我使用了request 但我找不到如何将正文添加到请求中。
有什么帮助吗?
我正在使用AWS SAM(Lambda和API网关)构建API,该合同的合同由第三方定义。
第三方使用主体中包含JSON的GET请求调用我的API。但是,将请求与正文发送到API时,CloudFront会拒绝该请求。
这是请求:
curl -X GET -H "Content-Type: application/json" --data '{"hello":"world"}' https://my-api.execute-api.us-east-2.amazonaws.com/Prod/my-api
Run Code Online (Sandbox Code Playgroud)
这是响应:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<TITLE>ERROR: The request could not be satisfied</TITLE>
</HEAD>
<BODY>
<H1>403 ERROR</H1>
<H2>The request could not be satisfied.</H2>
<HR noshade size="1px">
Bad request.
<BR clear="all">
<HR noshade size="1px">
<PRE>
Generated by cloudfront (CloudFront)
Request ID: 1p0St_-e3noQL-2uMxeB_2I6lkMr1mg5afvxJRmVpCdnG67Vgnhj9w==
</PRE>
<ADDRESS></ADDRESS>
</BODY>
</HTML>
Run Code Online (Sandbox Code Playgroud)
检查日志后,该请求不会命中API网关或Lambda函数。但是,如果我从请求中删除主体,则它将触发Lambda函数,并且会从API中获得相应的错误消息(告诉调用方期望的主体丢失了。)
curl -X GET -H "Content-Type: application/json" https://my-api.execute-api.us-east-2.amazonaws.com/Prod/my-api
Run Code Online (Sandbox Code Playgroud)
我正在通过SAM模板使用API网关的基本配置。这是相关的部分:
MyApiFunction:
Type: AWS::Serverless::Function
Properties: …Run Code Online (Sandbox Code Playgroud) amazon-web-services amazon-cloudfront aws-lambda aws-api-gateway
我有一个 spring(启动)服务器,想要使用 springdoc 从注释生成 OpenAPI 规范。
我有一个请求,请求正文中有两个参数。我希望第一个是必需的,第二个是可选的。
@RequestBody(required = {true|false})似乎只将主体中的所有参数设置为(不需要)。@Parameter另一方面,Javadoc说要使用io.swagger.v3.oas.annotations.parameters.RequestBody
这是我的代码,我希望生成一个规范,其中第一个参数是必需的,第二个参数是可选的:
@GetMapping("/fstVector")
public ResponseEntity<Vector> fstV(@RequestBody final Vector v1, @RequestBody(required = false) final Vector v2) {
return new ResponseEntity<>(v1, HttpStatus.OK);
}
@PostMapping("/fstVector")
public ResponseEntity<Vector> fstVPost(@RequestBody(required = true) final Vector v1, @RequestBody(required = false) final Vector v2) {
return new ResponseEntity<>(v1, HttpStatus.OK);
}
Run Code Online (Sandbox Code Playgroud)
然而,生成的规范需要两个参数:
/pond/fstVector:
get:
tags:
- circle-escape-controller
operationId: fstV
parameters:
- name: v1
in: query
required: true
schema:
$ref: '#/components/schemas/Vector'
- name: v2
in: query
required: …Run Code Online (Sandbox Code Playgroud) 我怎样才能像这样,将主体参数和带有授权令牌的标头发送到此
const searchByDate = async ({ date1, date2 }) => {
const tokenApp = window.localStorage.getItem('token');
const { data: res } = await axios.get(`${baseUrl}/search`, {
data: { date1: date1, date2: date2 },
headers: { Authorization: `${tokenApp}` },
});
return res;
};
Run Code Online (Sandbox Code Playgroud)
到目前为止,它向我抛出一个错误,缺少所需的请求正文
http ×2
http-delete ×2
javascript ×2
node.js ×2
php ×2
angular ×1
api ×1
aws-lambda ×1
axios ×1
c# ×1
dropwizard ×1
get ×1
httpclient ×1
httprequest ×1
io ×1
java ×1
jersey-2.0 ×1
laravel ×1
laravel-5 ×1
openapi ×1
request ×1
rest ×1
spring-boot ×1
springdoc ×1