删除API json缺少引号 [英] Delete API json missing quotes

查看:102
本文介绍了删除API json缺少引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于一个项目,我以json格式获取数据,所有CRUD也在json中。

删除记录时我需要这样的代码:

 [{
Id:1000
}]



这是正确的json。在Postman它工作正常。



在我的代码中我使用这样的代码:

 HttpClient client =  new  HttpClient(); 
client.BaseAddress = new Uri(url);
byte [] cred = UTF8Encoding.UTF8.GetBytes( 用户名:密码);
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue( Basic,Convert.ToBase64String(cred));
client.DefaultRequestHeaders.Accept.Add( new MediaTypeWithQualityHeaderValue( 应用/ JSON));

HttpContent content = new StringContent(data,UTF8Encoding.UTF8, < span class =code-string> application / json
);
HttpResponseMessage responseMessage = client.PostAsync(url,content).Result;

return responseMessage.ToString();



但我的内容每次都在变化。

来自

 [{
Id:1000
}]



to

 [{
Id:1000
}]



它以某种方式丢失双引号,因此返回204并且没有记录被删除。



我很难找到如何解决这个问题。



我尝试过:



我尝试了一些字符串操作,但要么我最终没有双引号或两个双引号



喜欢:

 [{
Id:1000
}]

解决方案

找到了解决方案。请注意,与Postman或Fiddler无关。这就是我的意思:我的代码不起作用,我需要代码。

(但是GG,感谢您的努力和时间。:-))



 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();
}

使用(var httpResponse =(HttpWebResponse)request.GetResponse()){message = httpResponse.StatusCode.ToString(); }


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:

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"
}]

解决方案

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(); }


这篇关于删除API json缺少引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆