简体   繁体   English

将Json字符串发布到Web API C#

[英]Posting Json string to web api c#

I'm quite new to handling json data and web apis but I'm currently creating an application to further my knowledge on them. 我是处理JSON数据和Web API的新手,但是我目前正在创建一个应用程序以进一步了解它们。

At the moment I can do get requests and these work fine however I'm having issues with posts. 目前,我确实可以收到请求,但这些工作正常,但是我在发帖方面遇到了问题。 I'm trying to post json data to Jira Confluence to create a new page. 我正在尝试将json数据发布到Jira Confluence以创建一个新页面。

On https://developer.atlassian.com/confdev/confluence-server-rest-api/confluence-rest-api-examples it gives the below code to make a curl request https://developer.atlassian.com/confdev/confluence-server-rest-api/confluence-rest-api-examples上,它提供了以下代码来进行curl请求

 curl -u admin:admin -X POST -H 'Content-Type: application/json' -d'{"type":"page","title":"new page", "ancestors":[{"id":456}], "space":{"key":"TST"},"body":{"storage":{"value": "<p>This is a new page</p>","representation":"storage"}}}' http://localhost:8080/confluence/rest/api/content/ | python -mjson.tool 

I am currently using the below code and it comes back with a status 200 but I don't see the created page on confluence. 我当前正在使用以下代码,并且返回的状态为200,但在融合时看不到创建的页面。 I was wondering if anyone could help me out 我想知道是否有人可以帮助我

HttpClient _webRequest = new HttpClient();
byte[] cred = UTF8Encoding.UTF8.GetBytes(credentials);
_webRequest.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(cred));
_webRequest.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
string response = "{\"type\":\"page\",\"title\":\"new page\",\"ancestors\":[{\"id\":40635756}],\"space\":{\"key\":\"VG\"},\"body\":{\"storage\":{\"value\":\"<p>This is a new page</p>\",\"representation\":\"storage\"}}}"

var content = new StringContent(lol, Encoding.UTF8, "application/json");
var result = _webRequest.PostAsync("http://jirasite/confluence/rest/api/content/", content).Result;

Your auth header looks incorrect... You are missing the whitespace after " Basic " it should be like this " Basic xxx ". 您的auth标头看起来不正确...您在“ Basic ”之后缺少空格,应该像这样的“ Basic xxx ”。

Please take a look at the following document: 请看以下文件:

https://developer.atlassian.com/confdev/confluence-server-rest-api/confluence-rest-api-examples?_ga=2.72183699.1804458933.1496916716-1816616270.1496916716#ConfluenceRESTAPIExamples-Createanewpage https://developer.atlassian.com/confdev/confluence-server-rest-api/confluence-rest-api-examples?_ga=2.72183699.1804458933.1496916716-1816616616270.1496916716#ConfluenceRESTAPIExamples-Createanewpage

Regards 问候

Craig 克雷格

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM