Hi,
Iāve been able to create a job using the SDK API.
However, Iām a bit confused when trying to create a job using the REST API. Iāve seen the documentation but Iām not sure where to get the following required parameters. These are not required when creating a job in the SDK API:
- method_type: Type to the method to run the App Job.
- editor_session: UUID of the current editor session. This field is required when called under public endpoints.
When I make a POST request without those parameters I just get a 405 Not Allowed status. Iāve tried the code here but I still get an error.
Hi Panji,
First of all, as long as you are not trying to create a job for a public workspace, the editor_session is not required. If you are using a public workspace, you should create the session using: Create a new Editor Session | VIKTOR Documentation. Also, the method_type is not needed for a normal view or button, if you are trying to trigger something else like a preprocess or step method, let me know.
The 405 Not allowed generally means that you are using the wrong HTTP method for the url, so make sure that you are indeed using a POST on the valid url.
Your code should look roughly like this (if you use python). This should give you the url where you can fetch the job
import requests
import json
url = "https://<environment>.viktor.ai/api/workspaces/<workspace_id>/entities/<entity_id>/jobs/"
payload = json.dumps({
"method_name": "<you_controller_method_name>",
"params": {<your params here>},
})
headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer <PersonalAccessToken>'
}
response = requests.post(url, headers=headers, data=payload)
print(response.json()["url"])
Hi @kvangiessen,
Thanks for the reply. It is now working. I think I was a bit tired at the time and did not notice the URL was wrong. I did not notice /api/
was missing in my URL.
I now get a 201 status.
Hi, just another question related to this.
Is it possible to return a JSON response via the REST API so we can return custom information when sending a request to the Job REST API?
Hi Panji,
Iām not sure I fully understand the question; our REST API always returns a JSON response (except for a 204 status).
Could you elaborate a bit more on the flow that you are envisioning?
Does this ācustom informationā need to be included when the job is created, or when the job completes and returns the result of the job?
Kind regards,
Hi, sorry let me clarify.
I know if we have poll_result=True
we can get a response.
{"uid":1,
"kind":"result",
"status":"success",
"completed_at":"2024-01-01T09:09:25.376000Z",
"error":{},
"result":{},
"message":{}
}
Given the JSON response above, is there a way we can include our own data in the message
key? Or create a custom key to show a custom message to users?
Ideally, I want to have something like this for example:
{"message": {"report_url": "https://xxx"}}"
or
{"message": "Custom message we will show"}
or maybe a custom key
{"report_url": "https://xxx"}
Hi Panji,
Currently, we do not support adding a custom message when the job has completed successfully (i.e. status = āsuccessā).
However, we do allow adding a āmessageā when the job is still in progress (i.e. status = ārunningā). You can set this message through: Docs: Non-interruptive message.
Note that, once the job completes successfully, the āmessageā object will be cleared and the āresultā object will be filled with the result.
I hope this helps you in your case, and if not, feel free to add a Feature Request.
1 Like