# Posting form data with function

**URL:** <https://discourse.nodered.org/t/posting-form-data-with-function/20790>\
**Category:** General\
**Created:** [24 January 2020 00:15 UTC](https://discourse.nodered.org/t/posting-form-data-with-function/20790 "2020-01-24T00:15:44Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![brunoamaral](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/brunoamaral/32/16112_2.png) [@brunoamaral](https://discourse.nodered.org/u/brunoamaral)\
**Post date:** [24 January 2020 00:15 UTC](https://discourse.nodered.org/t/posting-form-data-with-function/20790/1 "2020-01-24T00:15:44Z")

</div>

I'm trying to post a form to a service that receives 2 pieces of data, but for some reason the function for form data isn't working.

This is the function I have so far:

```auto
var payload = msg.payload;

var data = { 'url': payload.content,'format': 'bestvideo' };

msg.headers = {
'content-type':'application/x-www-form-urlencoded',
'boundary':'63c5979328c44e2c869349443a94200e'
}
msg.payload = {JSONString : JSON.stringify(data)}
return msg;

```

And the msg.payload returns what I expect as data:

`{"url":"https://www.youtube.com/watch?v=oY6_DSuYFg4","format":"bestvideo"}`

But my service returns an error saying that URL was not received.  
What am I missing?

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [24 January 2020 07:20 UTC](https://discourse.nodered.org/t/posting-form-data-with-function/20790/2 "2020-01-24T07:20:53Z")

</div>

Since you have given us no clue about node red interfaces with the service it is impossible to say.

---

<div class="post-metadata">

**Author:** ![brunoamaral](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/brunoamaral/32/16112_2.png) [@brunoamaral](https://discourse.nodered.org/u/brunoamaral)\
**Post date:** [24 January 2020 08:59 UTC](https://discourse.nodered.org/t/posting-form-data-with-function/20790/3 "2020-01-24T08:59:23Z")

</div>

you're right, I should have given more detail.

Right now, the flow only uses the function interface, with the code above and the http-request where I only set the method to POST and the URL.

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [24 January 2020 09:19 UTC](https://discourse.nodered.org/t/posting-form-data-with-function/20790/4 "2020-01-24T09:19:54Z")

</div>

Hi @brunoamaral the challenge is more that we don't know what the service API you are calling expects in terms of how you have to format the request.

As a start, I would remove the `msg.headers` settings and let the node encode it how it thinks it should be set.

The format of the payload is slightly unusual - you are posting a form with one property called `JSONString` whose value is the json encoding of the object with the url and format properties. Is that really what your service expects? Or should the body of the http request have the `url` and `format` properties at the top level? I would try setting `msg.payload = data` and seeing what happens (again, without the `msg.headers` property being set so the node sets the proper default header values).

---

<div class="post-metadata">

**Author:** ![brunoamaral](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/brunoamaral/32/16112_2.png) [@brunoamaral](https://discourse.nodered.org/u/brunoamaral)\
**Post date:** [24 January 2020 10:24 UTC](https://discourse.nodered.org/t/posting-form-data-with-function/20790/5 "2020-01-24T10:24:21Z")

</div>

Thank you @knolleary, that was it.

I still had to keep one of the headers, but the code below works:

```auto
var payload = msg.payload;

var data = { 'url': payload.content,'format': 'bestvideo' };

msg.headers = {
'content-type':'application/x-www-form-urlencoded'
}
msg.payload = data;
return msg;

```

---

<div class="post-metadata">

**Author:** ![gauravdasgupta](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gauravdasgupta/32/23372_2.png) [@gauravdasgupta](https://discourse.nodered.org/u/gauravdasgupta)\
**Post date:** [5 February 2021 07:28 UTC](https://discourse.nodered.org/t/posting-form-data-with-function/20790/6 "2021-02-05T07:28:09Z")

</div>

Heartfelt thanks for providing this solution here 😍

---

<div class="post-metadata">

**Author:** ![danielo515](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/danielo515/32/31899_2.png) [@danielo515](https://discourse.nodered.org/u/danielo515)\
**Post date:** [21 February 2021 15:40 UTC](https://discourse.nodered.org/t/posting-form-data-with-function/20790/7 "2021-02-21T15:40:53Z")

</div>

Following the http request documentation this is what worked for me:

```auto
const {payload} = msg

const headers = {
'content-type':'multipart/form-data'
}

return {
    headers,
    payload: {
        image: {
            value: payload,
            options: {
                filename: 'image.jpg'
            }
        }
        
    }
}

```

The input of the function node is a file-read node

---

<div class="post-metadata">

**Author:** ![alyssonalvesTI](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/alyssonalvesti/32/61543_2.png) [@alyssonalvesTI](https://discourse.nodered.org/u/alyssonalvesTI)\
**Post date:** [23 May 2022 19:35 UTC](https://discourse.nodered.org/t/posting-form-data-with-function/20790/8 "2022-05-23T19:35:07Z")

</div>

tanks ! sucess!
