繁体   English   中英

如何使用 curl 将 CSV 文件发布到弹簧休息端点

[英]How to post a CSV file to a spring rest endpoint using curl

我正在尝试使用接受 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"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM