Forums

How to send a command to a console with API?

Hi, I'm trying to send commands to a bash console with the API. for now, I'm just trying to send a simple command to get response. I'm using postman to check if the call is working but all I get is: { "detail": "Method \"GET\" not allowed." } (I'm using post command. not GET) or some html code of the site.

can someone help me with it? a simple example of a request with the params and headers will help me a lot.

Thanks!

How does your request look like? What is the url you are hitting?

Hi, I'm sending POST request to the url: https://www.pythonanywhere.com/api/v0/user/{username}/consoles/{id}/send_input/

and the post params make it be: https://www.pythonanywhere.com/api/v0/user/{username}/consoles/{id}/send_input/?input={input} for example: https://www.pythonanywhere.com/api/v0/user/{username}/consoles/{id}/send_input/?input=ls\n

In the Headers section - type: API Token; Key: Authorization; Value: Token {Token ID} Key: Content-Type; Value: application/json

I'm replacing {username}, {id}, {Token ID} with their values.

Could you send us real data on support@pythonanywhere.com. Maybe screenshot?

I sent you an email. Thanks.

Thanks, I believe we already contacted you.

How did you solve it?

@segu contact us at support@pythonanywhere.com with some more details of your problem.

How did you solve the issue because i have the same issue?

@TridecYouTube Contact support@pythonanywhere.com with some more details of your problem.

Hi, I am just wondering how to actually submit the text that you 'type' to the console as a command? I am able to successfully send the text to the console which I can view, but it is just sitting there and not submitting. Sorry if this is a dumb question...

What happens if you hit the return key after typing the command? Perhaps a screenshot would help work out what the problem is.

Hitting 'return' successfully submits the command. However, I am trying to have it submit the command via the API (ie. without actually going to the console and hitting enter once the text is 'typed' in the console by the API).

Thanks a lot.

Ah, sorry! I completely missed the topic of this forum thread. You should be able to get that to work by appending a newline ("\n") to the command.

Hi, no problem at all, I realized my post was not super clear after the fact.

Thanks for that, but unfortunately the "\n" just shows up in the console line as text for some reason as well...

Example using Input Parameter

The screenshot above is the somewhat successful result of setting a Bubble Parameter to "Input"

Bubble Screenshot

By contrast, I get an error stating "

Raw response for the API Status code 415 {"detail":"Unsupported media type \"\" in request."}"

if I try to use the JSON body as such:

JSON Bubble

Ah, I see -- I think the tool you're using to send the requests is converting the \n to the literal characters \ and n before you send it. Not sure about that second error, though -- I don't know what it's sending there.

Could you try hitting the API directly from Python code on your own machine? For example:

    response = requests.post(
        THE_URL,
        headers={'Authorization': 'Token {}'.format(YOUR_TOKEN)},
        json={'input':  "python HelloWorld.py\n"},
    )

...replacing THE_URL and YOUR_TOKEN appropriately, of course.

I will give that a shot and see if the Bubble forum has a workaround on their end to prevent that conversion.

Thanks very much!

Let us know if it works!

I actually just figured it out:

I set Body Type to "Raw", added a header for "Content-Type" value = "application/json"

and set the body to be simply: "{"input":"python helloworld.py\n"}"

I guess the pre-provided Bubble parameter fields 'sanitize' the inputs for lack of a better term

Glad to hear that you made it!

Does the console has to be open in my browser or something and then only will the api work? Or I can run the api whenever i want even if the console is not open? Cause when i run the code to type a command in console I get an error saying:

Got unexpected status code 412: b'{"error":"Console not yet started. Please load it (or its iframe) in a browser first"}'

The console does not have to be open in a browser, but it must have been started in a browser before you can send anything to it.

Ok so like after I open the console in my browser and then close it, then till how long can I keep the console inactive till the console closes and no longer accepts any api requests for consoles.

You could use selenium to open the console. Consoles don't stay open for more than a day or two so you'll need to build somthgin to check that the console is still open before you send a command.

Yeah I was thinking about that too. btw like for example if I schedule a task to run a command on the console using the api will that increase the time for the console to stay active?

Yeah I was thinking about that too. btw like for example if I schedule a task to run a command on the console using the api will that increase the time for the console to stay active?

The consoles don't close after a certain period of inactivity