Click here to Skip to main content
15,905,914 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
For a project I get data in json format and all CRUD is also in json.
When deleting a record I need code like this:
[{
	"Id": "1000"
}]

This is correct json. In Postman it works fine.

In my code I use code like this:
C#
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(url);
byte[] cred = UTF8Encoding.UTF8.GetBytes("username:password");
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(cred));
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

HttpContent content = new StringContent(data, UTF8Encoding.UTF8, "application/json");
HttpResponseMessage responseMessage = client.PostAsync(url, content).Result;

return responseMessage.ToString();

But my content changes everytime.
from
[{
	"Id": "1000"
}]

to
[{
	Id: "1000"
}]

It loses the double quotes in some way and so it returns a 204 and no record is deleted.

I'm struggeling to find out how to solve this.

What I have tried:

I have tried alle kind of string manipulations but either I end up with no double quotes or two double quotes

like:
[{
	""Id"": "1000"
}]
Posted
Updated 26-Nov-17 7:02am
v5
Comments
Graeme_Grant 5-Nov-17 16:54pm    
How are you creating the JSON data?
Wiep Corbier 5-Nov-17 17:02pm    
like this:

string data = @"[{" + "\"" + propertyName + "\"" + ": " + "\"" + @"" + value + "\"}]";

One of the problems to me is the [ and ] in this case.

Maybe I just need completely different code.

BTW: I'm doing .NET and the receiving end is PHP.
They say that api's are made for making life easier, but if everyody does it in a different way, the easy part is gone :-)
Graeme_Grant 5-Nov-17 17:26pm    
Firstly, you don't use verbatim strings for plain strings, so you can drop the @s (read more here: string (C# Reference) | Microsoft Docs[^]). Verbatim strings is only if you don't want to use escaped strings like "\"" - verbatim would be @"""".

For JSON data you should use JSON serialization. You can read more about them in this article: Working with JSON in C# & VB[^]

Your JSON that is hard-coded looks fine. What makes you think that it is not?
Wiep Corbier 6-Nov-17 2:22am    
Hi, The problem is: with or without the "@"

Second my biggest problem in coding is: get people to understand my question, although I think I explain it in a way people understand it. But no :-)

When I create a string like:
[{
"Id": "1000"
}]

in Visual Studio I can check what this is:
with the TEXT Visualizer it is:
[{"Id": "4"}]
with the XML Visualizer there is:
nothing
in HTML Visualizer there is:
[{"Id": "4"}]

In JSON Visualizer there is:
> [JSON]
> [0]
> Id: "4"

So, the " are gone and it results in 'no delete'. (204)

I can not make it much more clear.




Graeme_Grant 6-Nov-17 2:47am    
You are looking too hard at it. Each visualizer does it differently.

install Fiddler[^], run and peek at the traffic. There you will see what is actually sent. Alternatively, write it to a text file and open in NotePad.

1 solution

Found a solution. Mind you, had nothing to do with Postman or Fiddler. This is what I mean by: my code doesn't work, I need code that does.
(but G-G, thanks for the effort and time. :-) )

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "DELETE";
            request.ContentType = "application/json";
            request.Accept = "application/json";

            using (var streamWriter = new StreamWriter(request.GetRequestStream()))
            {
                streamWriter.Write(data);
                streamWriter.Flush();
            }

            using (var httpResponse = (HttpWebResponse)request.GetResponse()) { message = httpResponse.StatusCode.ToString(); }
 
Share this answer
 
Comments
Graeme_Grant 26-Nov-17 21:17pm    
Ah, so I was right, you needed to use DeleteAsync and not PostAsync ... Glad that it is sorted.
Wiep Corbier 27-Nov-17 0:20am    
No, in previous code DeleteAsync doesn't work. I'm not stupid.
Graeme_Grant 27-Nov-17 1:13am    
DeleteAsync adds request.Method = "DELETE";. You were using PostAsync which adds request.Method = "POST";.
Wiep Corbier 27-Nov-17 1:18am    
I know that in the code it says PostAsync, but I tried DeleteAsync also. I tried many things, it didn't work. That's why I posted the problem here. But. I also posted the solution and i want to leave it by that. Thanks again.

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