Skip to content

Instantly share code, notes, and snippets.

@dimobelov
Last active February 1, 2019 07:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dimobelov/ee70eb73762dcbdddb72cca307e6280c to your computer and use it in GitHub Desktop.
Save dimobelov/ee70eb73762dcbdddb72cca307e6280c to your computer and use it in GitHub Desktop.
GitLab API - Upload File
I created a function to also import issue attachements. I still need to add it to this lib. For anyone looking to do the work by yourself:
See: https://docs.gitlab.com/ee/api/projects.html#upload-a-file
private async Task<string> GitLabFileUploadAsync(string url, string token, string projectId, byte[] file, string fileName)
{
using (var httpClient = new HttpClient())
{
var form = new MultipartFormDataContent
{
{ new ByteArrayContent(file, 0, file.Length), "file", fileName }
};
form.Headers.Add("PRIVATE-TOKEN", token);
using (HttpResponseMessage response = await httpClient.PostAsync($"{url}/api/v4/projects/{projectId}/uploads", form))
{
response.EnsureSuccessStatusCode();
return response.Content.ReadAsStringAsync().Result;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment