Forms, do they work?

Does forms work? is there any examples how to setup a form somewhere? Docs says “Sorry, still in the making…”.

I have a good idea of how forms work. I’ll write some documentation.

4 Likes

@mpartipilo this would be awesome, thank you :ok_hand:

Hi, thanks! Is it possible you could show a simple example on how forms work? (using rest)

It’s done. Go check out the documentation page.

4 Likes

Thanks a lot:)

Btw, does this look correct? I’m getting a 404 on the postUrl.

		postForm() {
			const formData = new FormData(this.$refs.contactForm)
			const postUrl = `${this.cockpit.apiUrl}/forms/submit/Contact?token=${this.cockpit.apiToken}`

      		axios.post(postUrl, formData, {
				headers: { 'Content-Type': 'application/json' },
			}).then((response) => {
				console.log('response: ', JSON.stringify(response, null, 2))
			}).catch((error) => {
                console.log(error)
                console.log(formData)
            })
		}

At a glance, it looks ok.

Let me look for a working example code using axios.

This is how my http request looks from Postman. Axios is not listed in the code generators, but there’s an example with request.

POST /api/forms/submit/MyCockpitForm?token=mytoken HTTP/1.1
Host: content.mywebsite.com
Content-Type: application/json
Cache-Control: no-cache

{
    "form": {
        "field1": "value3",
        "field2": "value4"
    }
}

With request:
var request = require(“request”);

var options = { method: 'POST',
  url: 'http://content.mywebsite.com/api/forms/submit/MyForm',
  qs: { token: 'mytoken' },
  headers: 
   { 'Cache-Control': 'no-cache',
     'Content-Type': 'application/json' },
  body: { form: { field1: 'value3', field2: 'value4' } },
  json: true };

request(options, function (error, response, body) {
  if (error) throw new Error(error);

  console.log(body);
});

thanks, seems my url is wrong in some way but can’t understand what. I can’t access my form json in the browser with:
https://example.com/cockpit/api/forms/get/Contact?token=mytoken

It returns path not found. The exact url structure works for singletons and collections.

EDIT: Nevermind, works now after i did the correct form data structure according to the docs.

Thanks a lot for the docs:)

1 Like

@artur I was checking the code and it looks like API support in forms is limited to submit only. Can you confirm this?

I could not determine if there is an API to show the form’s entries.

yes, you’re right. the forms api only supports submits

Hello,

How can I receive the forms in my email?

I believe you can set up a webhook? Or you can go programmatically and implement a “forms.save.after” hook in a custom addon, I did something similar in the past for collections in that addon - https://github.com/pauloamgomes/cockpit-EmailOnSave

I can see my form entries, but I’m not getting any emails. Isn’t it enough to fill my email under “email” when editing the form?

Doesn’t it make sense to be able to GET form entries?

2 Likes

Checking the code I see that an endpoint was added: /api/forms/export/{form_name}?token=xxx

I like the idea of making the forms a proxy for a collection.

Mostly because I’d like to have pagination and validation in the call to retrieve form entries, instead of having to retrieve the whole list of entries with a single call.