Curl into https request - url

Hello, I was wondering how to convert the balena supervisor address into a https command. for example this:

curl --header "Content-Type:application/json" "$BALENA_SUPERVISOR_ADDRESS/v2/applications/$BALENA_APP_ID/restart-service?apikey=$BALENA_SUPERVISOR_API_KEY" -d '{"serviceName": "venue"}'

and break down the address so we can use it in the url:
HttpRequestMessage(new HttpMethod(type), $"http://{URL}/"))

Hey,

Following the balena docs you can access the supervisor using IP address:

127.0.0.1:48484

const URL = "127.0.0.1:48484";

So all of the “$BALENA_SUPERVISOR_ADDRESS/v2/applications/$BALENA_APP_ID/restart-service?apikey=$BALENA_SUPERVISOR_API_KEY” can just be “127.0.0.1:48484/v2/applications/$BALENA_APP_ID/restart-service?apikey=$BALENA_SUPERVISOR_API_KEY”?
what about the key, app id and restart service?

Well,

It depends you use case. but i if understood your problem correctly, doing from inside a container

HttpRequestMessage(new HttpMethod(type), "http://127.0.0.1:48484/v2/applications/$BALENA_APP_ID/restart-service?apikey=$BALENA_SUPERVISOR_API_KEY"))

Where $BALENA_APP_ID is the container id your want to restart and
$BALENA_SUPERVISOR_API_KEY is given by the hypervisor as an environment variable from the container.

This should do the trick.

for some reason I am getting 401 Unauthorized Response, my code:

public static async Task<string> htttpBalenaClientPostRequestAsync(string contentType, string type, string URL, string content, string authenticationToken)
        {
            using (var httpClient = new HttpClient())
            {
                using (var request = new HttpRequestMessage(new HttpMethod(type), $"http://127.0.0.1:48484/{URL}/"))
                {
                    request.Content = new StringContent(content);
                    request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse(contentType);
                    var response = await httpClient.SendAsync(request);
                    return response.StatusCode.ToString();
                }
            }
        }

the command in the url is:v2/applications/ $BALENA_APP_ID/restart-service?apikey=$BALENA_SUPERVISOR_API_KEY, and I echo for the Id and the Key, it is a post request. did I not add the key?

From this code, the key is missing.

If URL is:

v2/applications/ $BALENA_APP_ID/restart-service?apikey=$BALENA_SUPERVISOR_API_KEY

then $"http://127.0.0.1:48484/{URL}/" becomes:

$"http://127.0.0.1:48484/v2/applications/ $BALENA_APP_ID/restart-service?apikey=$BALENA_SUPERVISOR_API_KEY/" which is not quite right.

Perhaps try make sure there is no whitespace in URL, and change:

$"http://127.0.0.1:48484/{URL}/ to
$"http://127.0.0.1:48484/{URL}

so the last / doesn’t mess with your URL?

perhaps I need to use in the url only until service?

like this:

public static async Task<string> SetClientBalena (string type, string param, string content)
        {
            const string AUTHENTICATION_ENV_VAR = "BALENA_API_KEY";
            string balenaAppId = Environment.GetEnvironmentVariable("BALENA_APP_ID");

            string authenticationToken = Environment.GetEnvironmentVariable(AUTHENTICATION_ENV_VAR);
            
            string balenaURL = "http://127.0.0.1:48484/v2/applications/";
            balenaURL += balenaAppId;
            if (string.IsNullOrEmpty(authenticationToken))
            {
                Logger.LogError($"Failed getting Balena Environment Variable (Authentication) {AUTHENTICATION_ENV_VAR}");
                return null;
            }
           
            authenticationToken = "Bearer " + authenticationToken;
            using (var httpClient = new HttpClient())
            {
                httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", authenticationToken);
                using (var request = new HttpRequestMessage(new HttpMethod(type), balenaURL + param))
                {
                    request.Headers.TryAddWithoutValidation("Authorization", authenticationToken);
                    request.Content = new StringContent(content);
                    request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");

                    var response = await httpClient.SendAsync(request);
                    return response.StatusCode.ToString();
                }
            }
        }

where type is POST , content is: "{\"serviceName\": \"venue\"}" and my param is "/restart-service"