0

I have a Spring Boot server application that receives a POST request from an external service with the following characteristics:

Headers

accept-encoding: gzip,deflate
user-agent: Apache-HttpClient/4.3.6 (java 1.5)
connection: Keep-Alive
host: webhook.site
content-type: application/x-www-form-urlencoded
content-length: 558
Query strings: (empty)

Form values

BillNumber: 41492032464
BillValue: 600000.0
Description: Description

To be able to handle this POST request from the external service I implemented the following controller, which what it does is create and store an invoice, but my application returns an HTTP Error 406:

@RequestMapping(value = "/bills", method = RequestMethod.POST, headers = "Accept=application/x-www-form-urlencoded")
@ResponseBody
@Transactional
public void createBill(UriComponentsBuilder uriComponentsBuilder,
        final HttpServletRequest request,
        final HttpServletResponse response) throws IOException {
}

I understand that the error refers to the client (in this case the external service) does not understand the "language" of the server response, but as you can see in the headers of my controller I am accepting "application / x-www-form -urlencoded". I do not know if it's due to another problem also my controller it's void.

How should this controller be implemented in my spring boot app?

1
  • 1
    No, it means that the server can't generate what the client is asking for. You're mixing up Accept (here's what I want back) and Content-Type (here's what I'm sending) headers. Commented Nov 16, 2018 at 12:46

1 Answer 1

1

You should use consumes with MediaType to define the supported Content-Type. You can further simplify by using @PostMapping:

@PostMapping(value = "/bills", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)

It's pointless to set Accept header on response like you do now, this is a request header. As per Mozilla docs:

The Accept request HTTP header advertises which content types, expressed as MIME types, the client is able to understand. Using content negotiation, the server then selects one of the proposals, uses it and informs the client of its choice with the Content-Type response header. Browsers set adequate values for this header depending on the context where the request is done: when fetching a CSS stylesheet a different value is set for the request than when fetching an image, video or a script.

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

Comments

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.