Create Space API Error?

41 views
Skip to first unread message

Justin Freres

unread,
Aug 3, 2016, 9:40:17 PM8/3/16
to Assembla API Development
Any idea why this code would not work?  StatusCode: 406, ReasonPhrase: 'Not Acceptable'

           var c = JsonConvert.SerializeObject(space); 

            var content = new StringContent(c);

            var client = new HttpClient();
            client.BaseAddress = new Uri(baseURL + "/v1/spaces.json");
            client.DefaultRequestHeaders.Add("Accept", "application/json");
            client.DefaultRequestHeaders.Add("X-Api-Key", creds.ApiKey);
            client.DefaultRequestHeaders.Add("X-Api-Secret", creds.ApiSecret);
            HttpResponseMessage response = client.PostAsync(client.BaseAddress, content).Result;
            status = response.StatusCode.ToString();

Justin Freres

unread,
Aug 7, 2016, 5:17:00 PM8/7/16
to Assembla API Development
Support for assembla api is really lacking.  Solved the issue myself.  The 406 error is not helpful. 
It means that the json name space is not correct.. However, i was never able to get queryparams using httpclient to work correctly.. Switched to RestRequest. 
Hope this helps someone...

          string assemblaBaseUrl = "https://api.assembla.com/";
            RestClient assemblaClient = new RestClient(assemblaBaseUrl)
            {
             
            };
            var request = new RestRequest("/v1/spaces/"+ spaces.wiki_name + "/copy.json", RestSharp.Method.POST); // space template must be passed in to create using template
            request.RequestFormat = DataFormat.Json;
            request.AddHeader("Accept", "application/json");
            request.AddHeader("X-Api-Key", creds.ApiKey);
            request.AddHeader("X-Api-Secret", creds.ApiSecret);
            request.AddJsonBody(new { space = space });  // required to add a namespace to the serialized json object required by assembla
            var response = assemblaClient.Post<AssemblaSpace>(request);  // save the new space

Stanislav Kolotinskiy

unread,
Aug 8, 2016, 3:27:09 AM8/8/16
to assembla...@googlegroups.com
Hi Justin,

I'm sorry for the delay and for the inconvenience. Thanks for sharing the solution!

Regards,
Stanislav
--
You received this message because you are subscribed to the Google Groups "Assembla API Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to assembla-api-d...@googlegroups.com.
To post to this group, send email to assembla...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Justin Freres

unread,
Aug 8, 2016, 2:00:00 PM8/8/16
to Assembla API Development
What is wrong with the api adding a document to a space?  I have tried using Json Body and Query Params?

string path = @"/~App_Data/ProjectFiles/" + projectFilesToSpace.Guid.ToString() + "/" + item.Name;

// set file to stream

FileStream stream = new FileStream(item.FullName, FileMode.Open, FileAccess.Read);

BinaryReader reader = new BinaryReader(stream);

byte[] data = reader.ReadBytes((int)reader.BaseStream.Length);

spacefile.file = data;

spacefile.name = item.Name;

spacefile.folder_name = "Project Request Files";

string assemblaBaseUrl = "https://bigfiles.assembla.com";

RestClient assemblaClient = new RestClient(assemblaBaseUrl)

{

};

var request = new RestRequest("/v1/spaces/" + spaces.id + "/documents.json", RestSharp.Method.POST);

request.RequestFormat = DataFormat.Json;

request.AddHeader("Accept", "application/json");

request.AddHeader("X-Api-Key", creds.ApiKey);

request.AddHeader("X-Api-Secret", creds.ApiSecret);

request.AddHeader("Content-Type", "multipart/form-data");

//request.AddQueryParameter("document[file]","@" + item.FullName);

//request.AddQueryParameter("document[name]", item.Name);

//request.AddQueryParameter("document[folder_name]", spacefile.folder_name);

request.AddJsonBody(new { document = spacefile }); // required to add a namespace to the serialized json object required by assembla

var response = assemblaClient.Post<AssemblaSpace>(request); // save the new space document

Stanislav Kolotinskiy

unread,
Aug 9, 2016, 2:31:46 AM8/9/16
to Assembla API Development
Hi Justin,

there is nothing wrong with the API itself. You probably missed the notice at http://api-doc.assembla.com/content/ref/documents_create.html , which states the following:
To upload a file using the API add the Content-Type multipart/form-data header to your request headers, no JSON or XML data bodies is supported during file creation
So no JSON  body - you need to actually send data as a form. Please let me know if that helps.

Regards,
Stanislav

Justin Freres

unread,
Aug 10, 2016, 7:19:13 PM8/10/16
to Assembla API Development
Thanks.
Reply all
Reply to author
Forward
0 new messages