Remote Post Handler using "multipart/form-data"

Created on 4 May 2021, over 3 years ago
Updated 12 March 2024, 9 months ago

Problem/Motivation

I am using D8 Webforms as a front-end form for the Freshdesk.com customer support software. I am unable to post uploaded file attachments to Freshdesk.

Is it possible to use webform Remote Post Handler to post using "multipart/form-data" ?

Steps to reproduce

I have successfully configured a webform Remote Post Handler to send data from Drupal into a Freshdesk.com 'ticket' using Freshdesk's published API located here
https://developer.freshdesk.com/api/#tickets . I did this using Method=POST and Post type=JSON.

However, I am unable to post uploaded file attachments to Freshdesk. The Freshdesk API requires the Content-Type set to "multipart/form-data". The only two option I see for Remote Post Handlers in D8 webforms are "JSON" and "x-www-form-urlencoded".

✨ Feature request
Status

Needs work

Version

6.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States PaEricksonWaTech

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡¦πŸ‡ΊAustralia mstrelan

    We have a similar requirement for posting to Consultation Manager, but without the file attachment requirement. I've uploaded a patch to demonstrate that converting to multipart is relatively simply, but I'm not sure the best place to put this logic. It's borrowing directly from \Drupal\Tests\BrowserTestBase::translatePostValues to flatten the $request_data and then simply iterating over it again to convert to multipart.

    Example:

    [
      'a' => 'b',
      'c' => [
        'd' => 'e',
      ],
      'f' => [
        0 => [
          'g' => 'h',
        ],
        1 => [
          'i' => 'j',
        ],
      ],
    ];

    Is converted to:

    [
      0 => [
        'name' => 'a',
        'value' => 'b',
      ],
      1 => [
        'name' => 'c[d]',
        'value' => 'e',
      ],
      2 => [
        'name' => 'f[0][g]',
        'value' => 'h',
      ],
      3 => [
        'name' => 'f[1][i]',
        'value' => 'j',
      ],
    ];
    

    Needs work for test coverage and guidance on where this can live.

  • πŸ‡¦πŸ‡ΊAustralia mstrelan

    Attaching the patch this time 🀦

  • πŸ‡¦πŸ‡ΊAustralia mstrelan

    Had the wrong key in that patch, new one should be good.

  • Status changed to Needs review 8 months ago
  • πŸ‡ΊπŸ‡ΈUnited States jrockowitz Brooklyn, NY
  • πŸ‡ΊπŸ‡ΈUnited States jrockowitz Brooklyn, NY
  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 10.2.x + Environment: PHP 8.1 & MySQL 8
    32:25
    30:35
    Running
  • πŸ‡¦πŸ‡ΊAustralia mstrelan

    Not sure what the expected format is but for Consultation Manager we needed multi value fields (like checkboxes) to have the same form key without [] rather than an array of values.

  • Merge request !449Resolve #3212237 "Multipart" β†’ (Open) created by mstrelan
  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 10.1.4 + Environment: PHP 8.2 & MySQL 8
    last update 8 months ago
    537 pass
  • Merge request !450Resolve #3212237 "Multipart alt" β†’ (Open) created by mstrelan
  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 10.1.4 + Environment: PHP 8.2 & MySQL 8
    last update 8 months ago
    537 pass
  • πŸ‡¦πŸ‡ΊAustralia mstrelan

    Added two MRs and hid the patch. MR !449 is in the original format I attempted as per previous comments and MR !450 is in the format that actually worked for Consultation Manager. Test coverage demonstrates the converted formats.

  • Pipeline finished with Success
    8 months ago
    #141194
  • Pipeline finished with Success
    8 months ago
    Total: 2089s
    #141196
  • πŸ‡ΊπŸ‡ΈUnited States jrockowitz Brooklyn, NY

    @mstrelan Thank you for contributing both MRs with test coverage. One side note: I appreciate that you are using KernelTest, which has not been used much in the webform module's test coverage.

    It is challenging that a "multipart/form-data" post could/should have two different structures for array values. We are also not addressing how files would be included in a "multipart/form-data" post.

    Creating a new handler, which could extend the RemotePost handler, in a dedicated contributed module could be more straightforward to address the "multipart/form-data" post and the Freshdesk.com customer support software.

  • πŸ‡¦πŸ‡ΊAustralia mstrelan

    mstrelan Thank you for contributing both MRs with test coverage. One side note: I appreciate that you are using KernelTest, which has not been used much in the webform module's test coverage.

    No problem, I think in hindsight there are some existing functional tests that could be extended but I have backported this from the client project I'm working on and it's primarily to demonstrate what the patch does, assuming if it eventually makes it to webform it would go through some refactoring.

    It is challenging that a "multipart/form-data" post could/should have two different structures for array values. We are also not addressing how files would be included in a "multipart/form-data" post.

    I'm wondering if one way is more correct than the other but really only have the one use case to go by. I think if there was a general consensus on one or the other then we could proceed, but if not then it's probably not worthwhile. I have completely overlooked files, which I think was the original intent of this issue, sorry for that.

    Creating a new handler, which could extend the RemotePost handler, in a dedicated contributed module could be more straightforward to address the "multipart/form-data" post and the Freshdesk.com customer support software.

    Exactly, and this is what we have ended up doing as we have other massaging to do in the handler as well. But the biggest challenge with this is that the remotePost method is 80 lines long, so without patching webform we have to duplicate the whole function. It would be easier to extend if this was broken up in to smaller chunks. Having said that there is probably a fair bit that is not applicable for our custom handler and could be removed, such as how to handle the GET method, since we'll only use POST.

  • Pipeline finished with Success
    5 months ago
    Total: 213s
    #214761
  • Pipeline finished with Success
    5 months ago
    Total: 183s
    #214776
  • Pipeline finished with Success
    5 months ago
    Total: 214s
    #216779
  • Pipeline finished with Success
    5 months ago
    Total: 210s
    #217340
  • Pipeline finished with Success
    4 months ago
    Total: 183s
    #240502
  • Status changed to Closed: won't fix 10 days ago
  • πŸ‡ΊπŸ‡ΈUnited States jrockowitz Brooklyn, NY

    Let's not make the change since on Freshdesk.com is requiring this change. Instead you can create a dedicated Freshdesk.com webform handler.

Production build 0.71.5 2024