balena-io / open-balena

Open source software to manage connected IoT devices at scale
https://balena.io/open
GNU Affero General Public License v3.0
1.04k stars 164 forks source link

balena API endpoints for post requests not working #174

Closed James-kamwendo closed 11 months ago

James-kamwendo commented 11 months ago

Expected Behavior

Im trying to follow through the API documentation for balena and am using it in django, i've tried to get data from my balena server that is hosted on my local server in virtualbox, and the get request are working just fine but when i try to create say a fleet following the documentation the response is 500 but when i use the balena cli its creating the fleet without a problem so i thought maybe am doing it wrong in django and i tried to curl on terminal the Post request for creating a fleet and i did verbose the response was also 500 but the same endpoint am able to get the fleets i created on balena cli, i tried to curl the delete request this one worked but Posting no, i also tried to patch an existing fleet to Enable automatic update tracking for a fleet using curl it said bad request and ive been trying for 4 days now

Actual Behavior

Jamess-MacBook-Pro:~ jameskamwendo$ curl --cacert ca.crt -X POST "https://api.balenaproject.net/v6/application" -H "Content-Type: application/json" -H "Authorization: Bearer " --data '{"app_name": "fooled", "is_of_device_type": "raspberrypicm4-ioboard"}' -v Note: Unnecessary use of -X or --request, POST is already inferred.

Specifications

dfunckt commented 11 months ago

API logs should tell you what the issue is.

In this case and if what you pasted is accurate, it should be that is_of_device_type should be spelled is_of__device_type (note the double underscore between is_of and device_type because it's a relationship traversal).

James-kamwendo commented 11 months ago

Thank you for responding, the documentation is like this: curl -X POST \ "https://api.balena-cloud.com/v6/application" \ -H "Content-Type: application/json" \ -H "Authorization: Bearer " \ --data '{ "app_name": "", "device_type": "" }'

and i did this i was getting this: Jamess-MacBook-Pro:~ jameskamwendo$ curl --cacert ca.crt -X POST "https://api.balenaproject.net/v6/application" -H "Content-Type: application/json" -H "Authorization: Bearer HgZNAhxvD4O7kk1qfyJxavz1dmqZo1Xn" --data '{"app_name": "fooled", "device_type": "raspberrypicm4-ioboard"}' -v Note: Unnecessary use of -X or --request, POST is already inferred.

so i think i posted a log of what i was just trying after seeing the reponse when i created a fleet with balena cli the json had the is_of__device_type sort of so i thought maybe this might work just being desperate to see it work

dfunckt commented 11 months ago

is_of__device_type must be a number, the ID of the device type.

device_type as per documentation must be a string, the slug of the device type.

Either of the two should work, assuming the API knows about the raspberrypicm4-ioboard which I understand is a custom one.

James-kamwendo commented 11 months ago

Same reponse am getting in django, code snippet is like the following: if request.method == 'POST': api_url = "https://api.balenaproject.net/v6/application"

    app_name = request.POST.get('app_name')
    device_type = request.POST.get('device_type')

    headers_create = {
        "Content-Type": "application/json",
        "Authorization": f"Bearer {auth_token}"
    }

    payload = {
        "app_name": app_name,
        "device_type": device_type
    }

    print(payload)

    # device_type

    # response = requests.post(api_url, json=payload, headers=headers_create, verify=True)
    response = requests.request('POST', api_url, json=payload, headers=headers_create, verify=True)

    print("Response Content:", response)
James-kamwendo commented 11 months ago

is_of__device_type must be a number, the ID of the device type.

device_type as per documentation must be a string, the slug of the device type.

Either of the two should work, assuming the API knows about the raspberrypicm4-ioboard which I understand is a custom one.

I retried it with the is_of__device_type and put a number that was assigned to an existing fleet i created with balena cli and i still get this and same thing in postman:

Jamess-MacBook-Pro:~ jameskamwendo$ curl --cacert ca.crt -X POST "https://api.balenaproject.net/v6/application" -H "Content-Type: application/json" -H "Authorization: Bearer HgZNAhxvD4O7kk1qfyJxavz1dmqZo1Xn" --data '{"app_name": "fooled", "is_of__device_type": 65}' -v Note: Unnecessary use of -X or --request, POST is already inferred.

dfunckt commented 11 months ago

Sorry, I don't see what's wrong. I'll close the issue as the issue tracker is not for support questions, but please post your issue in our forums, you'll also reach a much wider audience there.