简体   繁体   中英

Method arguments in http put request (csv? Body with String?)

I am trying to write a method that will respond to this http request. Unfortunately, I do not know what the argument of such a method should be.

cat data.csv | curl --header "Content-Type: text/plain" --request PUT --data-binary @- http://localhost:8080/endpoint

Swagger:

put:
  tags:
  - "employees"
  summary: "Stores employees hierarchy tree on server"
  description: "Sends or replaces employees hierarchy tree on server"
  operationId: "endpoint"
  consumes:
  - "text/plain"
  parameters:
  - in: "body"
    name: "body"
    description: "CSV with four columns - employee id, employee name, employee surname and employee superior id. Columns are separated with ',' character."
    required: true
    schema:
      type: string
      example:[tons of employees data here]

At first I thought it would be a Multipartfile and then I would just process it (see dummy approach below), but then I saw a "string" in swagger and it confused me a bit. I have no clue anymore what the format of the input data is.

 @PutMapping("/endpoint")
public void readCSV(MultipartFile file) throws IOException {
    String content = new String(file.getBytes());
    System.out.println(content);
}

See https://www.baeldung.com/spring-request-response-body

Or you can access the HttpRequest directly and read the request body from HttpServletRequest How to access HttpServletRequest inside Spring component class

Multipart is possible as well, but then you can't use text/plain and you will also have to format your body according to the Multipart format.

I am facing the same thing right now. Actually, you have the information that it is String, text / plain (Swagger) and Content-Type "text / plain" which makes me believe that the transmitted data is just a String that was created from the data contained in the CSV file. So your method may look like this

@PutMapping("/endpoint")
public void readCSV(@RequestBody String csvData) 

and this csvData String will contain all the text from the CSV file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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