I just created the webform and added fields.

I need to create a Webform Submission Using Rest API POST method. I just installed Rest & Rest UI modules for this.

But when I try to test my API using API tester its showing the error like this :

{"message":"Could not determine entity type bundle: \u0022webform_id\u0022 field is missing."}

But I have already determined the webform_id and here is my configuration :

POST URL : http://example.com/entity/webform_submission?_format=json

Headers :

Content-Type : application/json

POST Data :

{
  "type": {
      "webform_id": "my_form"
    },
"your_mobile":[{"value":"123456789"}],
"your_name":[{"value":"Test"}],
"your_email":[{"value":"test@gmail.com"}]
}

Drupal Version : 8.6.4
Rest UI version : 8.x-1.16
Webform Module version : 8.x-5.1

Why its not working?

CommentFileSizeAuthor
#11 webfrompost.PNG24.72 KBsowndharya

Comments

Selva.M created an issue. See original summary.

wim leers’s picture

Project: REST UI » Drupal core
Version: 8.x-1.16 » 8.6.x-dev
Component: Code » rest.module
Category: Bug report » Support request
Priority: Major » Normal
Status: Active » Postponed (maintainer needs more info)
Issue tags: -REST API, -REST, -Webform submission +API-First Initiative

I agree this is a pain to figure out 😞

I recommend you to use https://www.drupal.org/project/jsonapi instead, its JSON structure is much clearer!

Try this instead:

{
  "webform_id": {
      "target_id": "my_form"
    },
"your_mobile":[{"value":"123456789"}],
"your_name":[{"value":"Test"}],
"your_email":[{"value":"test@gmail.com"}]
}
Selva.M’s picture

I just installed and configured the JSON API module https://www.drupal.org/project/jsonapi but its showing the error below :

{"errors":[{"title":"Internal Server Error","status":500,"detail":"The controller result claims to be providing relevant cache metadata, but leaked metadata was detected. Please ensure you are not rendering content too early. Returned object class: Drupal\\jsonapi\\ResourceResponse.","links":{"via":{"href":"http:\/\/example.com\/jsonapi\/webform_submission\/my_form"},"info":{"href":"http:\/\/www.w3.org\/Protocols\/rfc2616\/rfc2616-sec10.html#sec10.5.1"}},"source":{"file":"\/srv\/bindings\/eeb03678ba3e4b03a62651b67ec4650b\/code\/modules\/contrib\/jsonapi\/src\/EventSubscriber\/DefaultExceptionSubscriber.php","line":43}.............

Here is my Configuration :

POST URL : http://example.com/jsonapi/webform_submission/my_form

Headers :

Accept : application/vnd.api+json
Content-Type : application/vnd.api+json
Authorization : Basic c2VsdmEukZXN0aTEyMw==

POST Data :

{
  "data": {
"type": "webform_submission--my_form",
"your_mobile":[{"value":"123456789"}],
"your_name":[{"value":"Test"}],
"your_email":[{"value":"test@gmail.com"}]
}
}

I hope I have added this in a proper way. But why its not showing this error?

wim leers’s picture

Now you're mixing up two things. Can you please try what I suggested in #2 using the REST module first?

Selva.M’s picture

Hi, for your kind information I didn't mixing up, Here I just only used only JSON API and modified the POST Data according to that but got the error like above.

Also I have found that Webform_Rest Module for Webform submission through REST API.

What would you recommend?

Thanks.

wim leers’s picture

Here I just only used only JSON API and modified the POST Data according to that

Please don't use that. Use the REST API. Please send exactly what I suggested in #2. And then let me know the result.

jrockowitz’s picture

@Wim Leers I pinpointed the cause for the issue described in #3. Basically, webform submission label's support tokens and WebformSubmission::label is called very early in the call stack before any rendering. I only tested this issue via JSONAPI but I think the REST API would throw the same error.

@Selva.M Please review and test the patch provided in https://www.drupal.org/project/webform/issues/3010727#comment-12928067

wim leers’s picture

Status: Postponed (maintainer needs more info) » Closed (duplicate)
sowndharya’s picture

@Wim Leers I tried to send post method to create submission
Content is
{
"webform_id":{
target_id":"contact"
},
"name":[{"value":"Test"}],
"email":[{"value":"test@gmail.com"}],
"subject":[{"value":"123456789"}],
"message":[{"value":"hai hello"}]
}
and URL is http://local.com/jsonapi/webform_submission/contact
I used JSON API and REST module but I am getting 403 error(X-CSRF-Token request header is invalid). Is any changes in config side?.Please help me with this.

tsega’s picture

@sowndharya I believe you need to send the X-CSRF-Token in the header of your request. Here is an example of how to send it:

  const message = {
     'webform_id' : 'contact',
     'name' :[{'value': 'Test' }],
     'email' :[{'value': 'test@gmail.com'}],
     'subject' :[{'value': '123456789'}],
     'message' :[{'value': 'Hello'}]
  }

  const token = 'you_need_to_get_this_from_somewhere'

  fetch(`/entity/webform_submission?_format=json`, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'X-CSRF-Token': token
      },
      body: JSON.stringify(message)
    })

Notice: Simplifying the 'webform_id' property structure as above works for me or else I get an error saying it can't find the webform_id.

sowndharya’s picture

StatusFileSize
new24.72 KB

@tsega Thank you that error got resolved.
But I am getting 400 bad request
URL:http://example.com/jsonapi/webform_submission/contact
body: which you mentioned in comment #10
header: token,basic_auth,content-type,accept-type

sowndharya’s picture

tsega’s picture

@sowndharya what is the error message that you get in the response?

FYI, I haven succeeded in posting using webforms and REST module, looking for the solution myself. Will share here once it does work for me.

sowndharya’s picture

@tsega Error is 400 Bad Request.
Please share the steps once it is done
Thanks