Position Green API: Uploading files

Modified on Thu, 27 Mar at 11:35 AM

Inserting data into Position Green


To import files into Position Green you can HTTP POST a file to https://apiurl/imports.


You must also supply valid credentials in the form of an access token.


The request must have a multipart/form-data content type and the file sent as the variable file. This together with a filetype (string) identifies the type of imported file, if you have many different imports then it is used to identify which one to start the process of. See examples in the references chapter in the end for more detailed usage of how it is used.


Note about fileType values


fileType is a string value that needs to match an existing import-configuration key in Position Green.


These string values are provided by Position Green upon request. 


If additional import-configurations are added to the platform where API-upload is wanted the available fileType-value again needs to be requested.


Example 1 - Import a file to Position Green API using nothing but HTTP


namespace TestClient;
public class Program
{
    private static string apiUrl = "https://apiurl"; //Provided by Position Green on request
    private static string loginUrl = "https://oidcurl"; //Provided by Position Green on request
    public static async Task Main()
    {
        Console.Title = "Console Client Credentials Flow";
        var token = await RequestToken("clientid", "clientsecret"); //Provided by Position Green on request
        var fileType = "file type"; //Provided by Position Green on request
        await UploadFile(token, "data.xml", fileType);
    }
    private static async Task<string> RequestToken(string clientId, string clientSecret)
    {
        var client = new HttpClient();
        client.DefaultRequestHeaders.Add("Accept", "application/json");
        client.BaseAddress = new Uri(loginUrl);
        var grant_type = "client_credentials";
        var data = new Dictionary<string, string>
        {
            { "client_id", clientId },
            { "client_secret", clientSecret },
            { "grant_type", grant_type },
        };
        var postTask = client.PostAsync("/connect/token", new FormUrlEncodedContent(data));
        postTask.Wait();
        var response = postTask.Result;
        var token = await response.Content.ReadFromJsonAsync<Token>();
        return token.access_token;
    }
    static async Task UploadFile(string token, string file, string fileType)
    {
        var handler = new HttpClientHandler()
        {
            AllowAutoRedirect = false
        };
        var client = new HttpClient(handler)
        {
            BaseAddress = new Uri(apiUrl)
        };
        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
        var form = new MultipartFormDataContent();
        var fileData = File.ReadAllBytes(file);
        var byteArrayContent = new ByteArrayContent(fileData);
        byteArrayContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
        {
            FileName = file,
            Name = "file"
        };
        byteArrayContent.Headers.ContentType = MediaTypeHeaderValue.Parse("text/xml");
        form.Add(byteArrayContent, "file");
        form.Add(new StringContent(fileType), "fileType");
        var response = await client.PostAsync("/imports", form);
        if (response.StatusCode != HttpStatusCode.OK)
        {
            throw new Exception(await response.Content.ReadAsStringAsync());
        }
        return;
    }
}
public class Token
{
    public string access_token { get; set; }
}


Example 2 - Import a file to Position Green API using the library for managing OpenID Connect


namespace TestClient
{
   class ProgramIdentityModel
   {
       public static async Task Main()
       {
           var loginUrl = "https://oidcurl"; //Provided by Position Green on request
           var clientId = "clientid"; //Provided by Position Green on request
           var clientSecret = "testclient"; //Provided by Position Green on request
           var response = await RequestTokenAsync(loginUrl, clientId, clientSecret);
           var apiUrl = "https://apiurl"; //Provided by Position Green on request
           var fileToUpload = "data.xml";
           var fileType = "file type"; //Provided by Position Green on request
           var redirectUrl = await UploadFile(response.AccessToken, fileToUpload, apiUrl, fileType);
       }
       static async Task<TokenResponse> RequestTokenAsync(string loginUrl, string clientId, string clientSecret)
       {
           var disco = await DiscoveryClient.GetAsync(loginUrl);
           if (disco.IsError) throw new Exception(disco.Error);
           var client = new TokenClient(
               disco.TokenEndpoint,
               clientId,
               clientSecret);
           return await client.RequestClientCredentialsAsync();
       }
       static async Task<Uri> UploadFile(string token, string file, string apiUrl, string fileType)
       {
           var handler = new HttpClientHandler()
           {
               AllowAutoRedirect = false
           };
           var client = new HttpClient(handler)
           {
               BaseAddress = new Uri(apiUrl),
           };
           client.SetBearerToken(token);
           var form = new MultipartFormDataContent();
           var fileData = await File.ReadAllBytesAsync(file);
           var byteArrayContent = new ByteArrayContent(fileData);
           byteArrayContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
           {
               FileName = file,
               Name = "file"
           };
           byteArrayContent.Headers.ContentType = MediaTypeHeaderValue.Parse("text/xml");
           form.Add(byteArrayContent, "file");
           form.Add(new StringContent(fileType), "fileType");
           var response = await client.PostAsync("/imports", form);
           if (response.StatusCode != HttpStatusCode.OK)
           {
               throw new Exception(await response.Content.ReadAsStringAsync());
           }
           return response.Headers.Location;
       }
   }
}


Import JSON


The import configuration can be passed through the /imports/json endpoint if the configuration is in JSON format. In this case, the JSON configuration must be sent as a string in the content parameter. See the endpoint details below:


POST /imports/json

Explore the import section on our Swagger page.


Import base64 string

The import configuration can be passed through the /imports/base64 endpoint if the configuration is in Base64 string format. In this case, the Base64 configuration must be sent as a string in the content parameter. See the endpoint details below:


POST /imports/base64


Explore the import section on our Swagger page.

Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select at least one of the reasons
CAPTCHA verification is required.

Feedback sent

We appreciate your effort and will try to fix the article