问题
Attempting to create a wiki page on an Atlassian wiki. I previously was using a python script and this code worked no problem:
data = json.dumps({"type":"page", "data":"testData", "title":postTitle,"space":{"key":"EB"}, "body":{"storage":{"value": content,"representation":"storage"}}})
r = requests.post("https://estech.atlassian.net/wiki/rest/api/content/", data=data, headers=headers, auth=(confluenceLogin['username'], confluenceLogin['password']))
Now I'm trying to use the following grunt task configuration:
http: {
atlassianwiki: {
options: {
uri: atlassianURL + "/wiki/rest/api/content/",
headers: {"Content-Type": "application/json"},
auth: {
"user": confluencelogin,
"pass": confluencepass
},
method:"POST",
body: JSON.stringify(wikijson)
}
}
}
with wikijson looking like:
wikijson = {
"type": "page",
"data": "testData",
"title": "testtitle",
"space": {key:"EB"},
"body": {
"storage": {
"value": "<p>testing posting</p>",
"representation": "storage"
}
}
}
And I get the following error when this task runs:
Fatal error: 500 {"statusCode":500, "message":"java.io.EOFException: No content to map to Object due to end of input"}
Upon a bit of google-fu, I found that some people claim they fixed this by adding "--post302" to their curl command line. But I don't really know or understand how that applies here.
回答1:
i was fighting with confluence REST API and in my case the problem was in content-type header, but you seem to have it already.
I didn't try to create new page but to update existing one
Confluence API seemed a little bit magic to me, so i just leave here all steps i had to make before it started working, maybe one of them will help you.
function composeRequest(method) {
var auth = new Buffer(user + ':' + pass).toString('base64');
var request = {
host: 'confluence.myserver.com',
port: 443,
contentType: "application/json; charset=utf-8",
'path': path,
method: method || "GET",
headers: {
'Authorization': 'Basic ' + auth,
'Content-Type': 'application/json'
},
rejectUnauthorized: false,
requestCert: true,
agent: false
};
return request;
}
And it appeared that page update request JSON MUST contain
- pageId (even it is inside path, you need to repeat it)
- type
- title
- version (it's weird, but you should set it. 0 or 1, i don't remember)
And when your data if filled, you should convert it to string and fill content-type field in your request!
data = JSON.stringify(data);
request.headers['Content-Length'] = data.length;
https.request(request, respondHandler)
来源:https://stackoverflow.com/questions/26744858/unexpected-grunt-http-error-when-posting-to-atlassian-confluence-api