简体   繁体   English

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

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

I'm trying to create a rest endpoint with java spring that accepts a csv file.我正在尝试使用接受 csv 文件的 java spring 创建一个休息端点。

My Controller looks like this:我的控制器看起来像这样:

Interface:界面:

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 is a static final String holding my endpoint url *CSV.BASE 是保存我的端点 url 的静态最终字符串

Implementation:执行:

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);
  }
}

I want to test this endpoint with the folowing sh script executet on a windows pc using git bash:我想使用 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

When I execute the script my controller Method is called.当我执行脚本时,我的控制器方法被调用。 However, setting a breakpoint shows me that the parameter "file" is always null.但是,设置断点显示参数“文件”始终为空。

I suspect there is something worng with the syntax of the file Path in the curl script, therefore I tried several things including absolute and relative paths.我怀疑 curl 脚本中文件路径的语法有问题,因此我尝试了几种方法,包括绝对路径和相对路径。 Of cause the error could also originate from my controller class.当然,错误也可能源自我的控制器类。

EDIT:编辑:

Adding the -vv option to the curl call resulted in this output:将 -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

I think this should work as it needs to be: Content-Type: multipart/form-data according to the code.我认为这应该可以正常工作: 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.

相关问题 REST POST端点以下载csv文件 - REST POST endpoint to download csv file Spring - 对返回文件的 Rest Endpoint 执行 Post 请求 - Spring - Doing a Post Request to a Rest Endpoint which returns a file 如何使用弹簧启动执行器分别捕获给定REST端点的GET,POST和PUT方法的度量 - How to capture metrics Separately for GET,POST and PUT method for given REST Endpoint using spring boot actuators 如何使用Postman从curl发布REST - How to Post a REST from curl using Postman 如何在没有@SpringBootApplication 的情况下,仅使用XML 文件在Spring Boot Java 中创建一个REST 端点? - How to create a REST endpoint in Spring Boot Java without @SpringBootApplication, using only XML file? Java/Spring - 发送 Post 请求以从另一个 Rest 端点检索 PDF 文件 - Java/Spring - Send Post Request to retrieve a PDF-File from anoter Rest Endpoint 如何使用curl使用2个参数进行POST? 休息。 Java的 - How to POST with 2 parameters using curl? REST. Java 如何使用Spring集成实现POST http端点? - How to implement POST http endpoint using spring integration? 使用 Spring REST 端点提供 yaml 文件 - Serve yaml file with Spring REST endpoint 使用Jquery向REST端点发送POST请求 - Sending POST request to REST endpoint using Jquery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM