2

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

2 Answers 2

0

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

1 Comment

I cannot edit the request. I have to conform to it. The whole problem is with this part: @PutMapping("/endpoint") public void readCSV( > WHAT ARGUMENT SHOULD BE HERE < ) { } I will try with HttpServletRequest and retrieving data from Collections

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.