--data-urlencode curl to nodeJS request or express module

Hello, I am trying to convert this string to curl

curl '<url>' -X POST 
--data-urlencode 'To=<phone>' 
--data-urlencode 'From=<phone>' 
--data-urlencode "Body=<message>" 
-u <user>:<pass>

into this nodejs code

var request = require('request');

var options = {
    url: 'url',
    method: 'POST',
    auth: {
        'user': 'user',
        'pass': 'pass'
    }
};

function callback(error, response, body) {
    if (!error && response.statusCode == 200) {
        console.log(body);
    }
}

request(options, callback);

I do not understand how to add the -data-urlencode parameter to the nodejs version of this code. Any hint on how I can do this?

+4
source share
1 answer

From the twist documentation :

- data-urlencode

(HTTP) Data for messages, similar to other parameters -d, --data, with an exception that performs URL encoding.

So, you should use a formform encoded as follows to submit the URL:

var options = {
    url: 'url',
    method: 'POST',
    auth: {
        'user': 'user',
        'pass': 'pass'
    },
    form: {
        To: 'phone',
        From: 'phone',
        Body: 'message'
    },
    headers: {
        'Accept': '*/*'
    }
};

, request-debug, , , :

To=phone&From=phone&Body=message

, curl, , --trace-ascii /dev/stdout:

curl '<url>' -X POST --data-urlencode "Body=<message>" -u <user>:<pass>  --trace-ascii /dev/stdout
+3

Source: https://habr.com/ru/post/1671055/


All Articles