简体   繁体   中英

PHP Curl CURLOPT_POSTFIELDS alternative for node.js

I'm looking for a way to interact with a form before submitting a POST like the CURLOPT_POSTFIELDS option for PHP curl provides, but with node.js.

I'm rewriting some PHP code to javascript, and it's the following line I can't find a javascript alternative for

curl_setopt($ch, CURLOPT_POSTFIELDS, '{"rememberMe": true}');

I'm currently using the node-fetch NPM library

My javascript code so far

fetch(requestUrl,
  {
    method: 'POST',
    headers: headers,
  }
)

with the headers including content-type and authorization

Thanks to @tadman the answer was found at https://www.npmjs.com/package/node-fetch#post-with-json

By providing a body you can achieve the desired result

Final code would look like the following if JSON is the desired way to go

fetch(requestUrl,
    {
        method: 'POST',
        body: JSON.stringify({'rememberMe': 'true'}),
        headers: headers
    }
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM