java - 如何使用 curl 将 CSV 文件发布到 spring rest 端点
问题描述
我正在尝试使用接受 csv 文件的 java spring 创建一个休息端点。
我的控制器看起来像这样:
界面:
package my.company.my.project.trms.controller;
import my.company.my.project.trms.controller.common.ControllerUrls.INBOX.CSV;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
@RestController
@RequestMapping(CSV.BASE)
public interface CsvController {
@PostMapping(produces = "text/csv", consumes = "text/csv")
public ResponseEntity create(@RequestBody MultipartFile file);
}
*CSV.BASE 是一个包含我的端点 url 的静态最终字符串
执行:
package my.company.my.project.trms.controller;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
@Slf4j
@RequiredArgsConstructor
@RestController
public class CsvControllerImpl implements CsvController {
@Override
public ResponseEntity create(MultipartFile file) {
String message = "";
return ResponseEntity.status(HttpStatus.OK).body(message);
}
}
我想使用 git bash 在 windows pc 上执行以下 sh 脚本来测试这个端点:
#!/bin/bash
curl -X POST "http://localhost:8791/api/public/v1/inboxes/csv" -H "accept: */*" -H "Content-Type: text/csv" --data-binary @/c/Users/Schilling/Desktop/Test.csv
当我执行脚本时,我的控制器方法被调用。但是,设置断点显示参数“文件”始终为空。
我怀疑 curl 脚本中文件路径的语法有问题,因此我尝试了几件事,包括绝对路径和相对路径。当然,错误也可能来自我的控制器类。
编辑:
将 -vv 选项添加到 curl 调用会产生以下输出:
Note: Unnecessary use of -X or --request, POST is already inferred.
* Uses proxy env variable no_proxy == '192.168.99.100'
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0* Trying 127.0.0.1:8791...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 8791 (#0)
> POST /api/public/v1/inboxes/csv HTTP/1.1
> Host: localhost:8791
> User-Agent: curl/7.65.3
> accept: */*
> Content-Type: text/csv
> Content-Length: 2036
> Expect: 100-continue
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 100
} [2036 bytes data]
* We are completely uploaded and fine
100 2036 0 0 100 2036 0 221 0:00:09 0:00:09 --:--:-- 0* Mark bundle as not supporting multiuse
< HTTP/1.1 200
< X-Content-Type-Options: nosniff
< X-XSS-Protection: 1; mode=block
< Cache-Control: no-cache, no-store, max-age=0, must-revalidate
< Pragma: no-cache
< Expires: 0
< X-Frame-Options: SAMEORIGIN
< Content-Type: text/csv;charset=UTF-8
< Content-Length: 0
< Date: Fri, 15 Oct 2021 12:37:43 GMT
<
100 2036 0 0 100 2036 0 203 0:00:10 0:00:09 0:00:01 0
* Connection #0 to host localhost left intact
解决方案
我认为这应该可以正常工作: Content-Type: multipart/form-data 根据代码。
#!/bin/bash
curl -vv -F upload=@/c/Users/Schilling/Desktop/Test.csv "http://localhost:8791/api/public/v1/inboxes/csv"
推荐阅读
- .net - .net 核心 httpclient 处理带有附加文件的响应
- c# - 我可以在 C#/VS2019 中找到变量的引用及其映射的引用吗?
- python-3.x - 为什么 sklearn.metrics.silhouette_score 对于稀疏矩阵输入这么慢?
- python - sum = (1**2) + (2**2) - (3**2) + (4**2)-,...,+(n**2) python中的程序代码
- ruby-on-rails - 无法注销 Rails 应用程序设计给出错误找不到路径“/tuners/sign_out”的设计映射
- c++ - 停止不断增加的无限递归模板实例化,这是不需要的
- vb.net - vb.net 以编程方式添加事件处理程序
- python - 对多个时间序列进行分组和聚合
- selenium - 从 './plugins' 导入 { PluginConfig, ProtractorPlugin };
- c# - 使用 netcoreapp3.0 安装 dotnet-ef 时面临的问题