Click here to Skip to main content
15,905,914 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am receiving from another website a curl request with json.
An example request would be
curl -X PUT -H "Content-Type: application/json" -d '{"c_id":"487672","amount":10}' http://xxxx/api/b2

This is just some test code to try and see the values...
public void Confirmation(HttpRequestMessage request)
    var content = request.Content;
    string jsonContent = content.ReadAsStringAsync().Result;
    CreateUpdate cpu = JsonConvert.DeserializeObject<CreateUpdate>(jsonContent);
    string acuid = cpu.c_id;

If I run using the above curl the value of the jsonContent string is

And as a result the code breaks and the acuid string is not populated.

What I have tried:

As a test I changed the curl to
curl -X PUT -H "Content-Type: application/json" -d "{'c_id':'487672','amount':10}' http://xxxx/api/b2

i.e. replacing the double quotes with single quotes.

This time my acuid variable is populated correctly.

My question is, what do I need to do to allow the original curl to be read without losing it's quotes.
Updated 17-Aug-20 3:15am
F-ES Sitecore 17-Aug-20 7:26am    
I think this is just a quirk with the Windows version of cURL, it needs the outer quotes to be double and the inner to be single.

You have to escape double quotes within a JSON string:

But ... to get it through the CMD processor as well, you may have to additionally escape the escape character:
Share this answer
Bullgill Coder 17-Aug-20 7:45am    
My problem here is that I don't have any control over the originating JSON string, so I need to make my code work with supplied JSON.
OriginalGriff 17-Aug-20 8:22am    
The problem is that the quotes are reused - the command processor is "feeding" on them to use as delimiters for it's string processing, as well as the JSON expecting it.
To get "raw JSON" through the command processor, you have to escape them.
It's not pretty, but I've added the following line in to put the quotes back in...
jsonContent = jsonContent.Replace("'{", "{'").Replace(":", "':'").Replace(",", "','").Replace("}'", "'}"); 
I've done this right before I deserialise and it is able to populate my variables correctly.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900