I have a .NET Core 2.1 Web API controller and method that is supposed to consume POST XML requests from an external service via HTTP. Below is the method header of the controller action.
[HttpPost]
[Produces("application/xml")]
public async Task<IActionResult> PostReceivedMessage([FromBody] ReceivedMessage receivedMessage)
I wrote up a custom XML input formatter to process the XML request that works just fine when I post a sample XML request from Postman to the app's controller action. But when the service sends a similar request, the response from the app has the status 400, Bad Request.
After some debugging, I discovered that the requests come in with
Content-Type: application/x-www-form-urlencoded
instead of application/xml or text/xml as one would likely expect. The same behaviour is exhibited by the app if I change the header to match the content-type in the request sent by the external service.
I assume that x-www-form-urlencoded is meant for form data because model binding doesn't work when I change the action header to:
public async Task<IActionResult> PostReceivedMessage([FromForm] ReceivedMessage receivedMessage)
Since I have no control over the external service, how should I make the controller action able to process XML requests with x-www-form-urlencoded as the content-type?
UPDATE: Below is a sample request:
POST /check/api/receivedmessages HTTP/1.1
Cache-Control: no-cache
Pragma: no-cache
User-Agent: Java/1.7.0_45
Host: xxx.xxx.xxx.xxx
Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
Connection: keep-alive
Content-type: application/x-www-form-urlencoded
Content-Length: 270
<Request><requestId>95715274355861000</requestId><msisdn>345678955041</msisdn><timeStamp>2019/10/20 02:23:55</timeStamp><keyword>MO</keyword><dataSet><param><id>UserData</id><value>VHVqrA==</value></param><param><id>DA</id><value>555</value></param></dataSet></Request>