Atef Sdiri
Atef Sdiri

Reputation: 61

Using C# to update a Confluence page I keep getting 405 ERROR

I am trying to update a Confluence page. I have been able to use this on Confluence localhost, but when I tried it on the production server I got this error:

StatusCode: 405, ReasonPhrase: 'Method Not Allowed', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
  Vary: Accept-Encoding
  Date: Tue, 31 Jan 2017 21:29:44 GMT
  Server: Apache/2.2.15
  Server: (CentOS)
  Content-Length: 342
  Allow: GET
  Allow: HEAD
  Allow: POST
  Allow: OPTIONS
  Allow: TRACE
  Content-Type: text/html; charset=iso-8859-1
}

This is my code. Any idea what would be causing this issue?

string json = "{\"version\":{\"number\":4},\"title\":\"Bloomberg Test\",\"type\":\"page\",\"body\":{\"storage\":{\"value\":\"Hello World\",\"representation\": \"storage\"}}}";      
string userpass = username+":"+password;
string encoded = Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(userpass));
string encval = "Basic " + encoded;
var content = new StringContent(json, Encoding.UTF8, "application/json");
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Add("X-Atlassian-Token", "nocheck");
client.DefaultRequestHeaders.Add("Authorization", encval);
client.DefaultRequestHeaders.Add("Accept", "application/json");
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
client.BaseAddress = new Uri(baseurl);
var resp = client.PutAsync(@"/rest/api/content/"+pageid, content);

Upvotes: 0

Views: 691

Answers (3)

Atef Sdiri
Atef Sdiri

Reputation: 61

Replaced:

client.BaseAddress = new Uri(baseurl);
var resp = client.PutAsync(@"/rest/api/content/"+pageid, content);

With:

var resp = client.PutAsync(baseurl+"/rest/api/content/"+pageid, content);

My guess, the BaseAddress is doing something odd like adding a slash at the end or something.

It works now!

Upvotes: 1

kloarubeek
kloarubeek

Reputation: 2844

You're performing an HTTP Put via client.PutAsync(). This is probably allowed locally, but on the server it's not. The response even includes the allowed http methods:

Allow: GET Allow: HEAD Allow: POST Allow: OPTIONS Allow: TRACE

So if the suggested PostAsync() is not supported by Confluence, adjust the server and allow the PUT method as well.

Upvotes: 0

Richard Schneider
Richard Schneider

Reputation: 35477

405 means that the HTTP method (GET, POST, PUT ...) is not allowed.

I don't know the details of Confluence, but try using a POST request

var resp = client.PostAsync(@"/rest/api/content/"+pageid, content);

Upvotes: 1

Related Questions