Overview
If you are testing your api with Curl, you should be aware of this issue, escaping double quote in JSON data.Problem
Most of the documentation and blog on the InterWeb, mention the following format which I suppose works in Linuxcurl -H "Content-Type: application/json; charset=utf-8" -X POST --data '{"Name":"foo foo", "Id":1}' https://localhost:44399/api/values curl -H "Content-Type: application/json; charset=utf-8" -X POST --data "{\"Name\":\"foo foo\", \"Id\":1}" https://localhost:44399/api/values
These formats do not work in a Windows MS-DOS prompt.
Solution
In a Windows MS-DOS prompt, use the following# Solution 1, by escaping the " the right way curl.exe -H "Content-Type: application/json; charset=utf-8" -X POST --data "{""Name"":""foo foo"", ""Id"":1}" https://localhost:44399/api/values # Solution 2, by piping the json data echo {"Name":"foo foo", "Id":1} | curl.exe -H "Content-Type: application/json; charset=utf-8" -X POST -d @- https://localhost:44399/api/values # Solution 3, store the json data into a file curl.exe -H "Content-Type: application/json; charset=utf-8" -X POST -d @data.json https://localhost:44399/api/values
In a Windows PowerShell prompt, use the following
# With PowerShell make sure to call curl.exe curl.exe -H "Content-Type: application/json; charset=utf-8" -X POST --data "{`"`"Name`"`":`"`"foo foo`"`", `"`"Id`"`":1}" https://localhost:44399/api/values # Another sample with PowerShell cls $contentType = "Content-Type: application/json; charset=utf-8" $data ='{ `Guid`:`cd7af44d-db7b-4d4c-9157-052ce5f50836`,`FirstName`:`Sonny`,`LastName`:`Haking`,`Email`:`shaking0@theguardian.com`,`Gender`:`Male`,`Phone`:`310-632-6062`,`IpAddress`:`138.27.230.192`,`Country`:`Indonesia`,`Amount`:`$91.37`,`CC_Number`:`4026367644878790`,`CC_ExpMonth`:12,`CC_ExpYear`:2022,`CC_SecCode`:233}' $data = $data.replace("``","""""").replace("""","``""") $url = "https://localhost:44399/api/Donation" $command = "curl.exe -H `"$contentType`" -X POST --data `"$data`" $url " Write-Host $command Write-Host "" Invoke-Expression $command
This issue can make it very difficult to troubleshoot asp.net [FromBody] auto mapping feature.
It cost me 3 hours of my life.
public class DTO { public string Name { get; set; } public int Id { get; set; } } [Route("api/[controller]")] [ApiController] public class ValuesController : ControllerBase { [HttpPost] public IActionResult Post([FromBody] DTO dto) { Console.WriteLine($"POST dto name {dto.Name}"); return Ok(); } }
No comments:
Post a Comment