Comments (9)
You're probably best off looking at something like https://github.com/form-data/form-data which can do this for you (albeit asynchronously) It's quite complex and I haven't ever tried to implement it myself.
from sync-request.
Thanks for the suggestion of form-data. For anyone else that runs into this, this worked for me:
function postCsv(csvBuffer, path) {
const form = new (require('form-data'))
const request = require('request')
form.append('file', csvBuffer)
const p = new Promise(function(resolve, reject) {
form.submit({
// some custom destination settings
host: getApiHost(),
port: getApiPort(),
protocol: getApiProtocol() + ':',
path: path,
headers: { 'X-AUTH-TOKEN': auth } // will merge your custom headers with the ones it sets
}, function(err, res) {
if(res.statusCode > 300) {
console.log('Failed to upload csv:')
console.log(err)
console.log(res.statusCode())
}
resolve()
})
})
return p
}
from sync-request.
It's great that the solution worked for you. For what it's worth, this is not "synchronous" but rather promise based and asynchronous. This is actually much much better than doing it synchronously. See the note in the README:
You should not be using this in a production application. In a node.js application you will find that you are completely unable to scale your server. In a client application you will find that sync-request causes the app to hang/freeze. Synchronous web requests are the number one cause of browser crashes. For production apps, you should use then-request, which is exactly the same except that it is asynchronous.
You would be much better off using form-data
for multi-part form uploads and then-request for all other requests and just not using sync-request.
from sync-request.
Thanks for the clarification (promise async vs sync). In regards to using then-request instead of sync-request, my use case is for certain testing tasks where I need to run api calls for pre-setup/tear-down of the tests in a very specific order. I decided to use this module instead of then-request so I wouldn't have to worry the a-synchronous-ness of it. (I'd rather spend my time writing the tests than worrying about why api call B ran before api call A during setup)
Just wanted to give some insight on my use case. sync-request works really well for what we're using it for.
from sync-request.
Excellent, that's exactly the right use case for this library. I am also working on supporting synchronous multi-part form upload so that should be here soon.
from sync-request.
Also trying to send POST synchronously for testing purpose ... I've found this discussion matching what I'm looking for, but seems not to have a solution. Do things have changed since Oct 2015?
My aim is do a POST with Cookies and form-data.
I tried the following with no success :
const request = require('sync-request')
const FormData = require('form-data')
let form = new FormData()
form.append('name', 'Brand new project')
form.append('description', 'POSTed project description')
form.append('csrfmiddlewaretoken', csrfToken)
let res = request('POST', 'http://localhost:9090/api/projects/', {
headers: {
Cookie: 'csrftoken=' + csrfCookie + '; sessionid=' + sessionidCookie
},
form: form
})
When capturing this with wireshark, apparently the form is not included.
from sync-request.
You need to use the FormData that comes with sync-request
:
const request = require('sync-request')
const FormData = request.FormData;
let form = new FormData()
form.append('name', 'Brand new project')
form.append('description', 'POSTed project description')
form.append('csrfmiddlewaretoken', csrfToken)
let res = request('POST', 'http://localhost:9090/api/projects/', {
headers: {
Cookie: 'csrftoken=' + csrfCookie + '; sessionid=' + sessionidCookie
},
form: form
})
from sync-request.
Great, that was it! Thanks 👍
from sync-request.
Hey @ForbesLindesay can you please help - I still get 415 error on this request -
const request = require('sync-request')
const FormData = request.FormData;
let form1 = new FormData()
form1.append('grant_type', '*****')
form1.append('username', '******')
form1.append('password', '******')
let res = request('POST', 'https://********healthsuite.com/authorize/oauth2/token', {
headers: this.getAppHeaders(), form :form1
})
console.log(res)
from sync-request.
Related Issues (20)
- sync-rpc breaking Node 6 HOT 1
- Doesn't work with NW.js HOT 1
- How to make "timeout" take effect HOT 1
- An in-range update of cross-env is breaking the build 🚨 HOT 1
- error: connect ECONNREFUSED 127.0.0.1:port for request inside exit event listener for v6.1.0 HOT 5
- `proxy` option doesn't work
- Possibly leaked handle? HOT 2
- Error thrown when changing HTTP method to HEAD instead of GET HOT 2
- how can this be 'sync' if it returns a promise? HOT 1
- Request headers are not honored on the redirections followed from the original resource.
- HTTP Bearer token authorisation missing
- sync-request import blocking in worker_threads
- Error: read ECONNRESET
- Doesn't work when used in VS Code plugin HOT 2
- `sync-request` leaves orphaned `sync-rpc` processes
- Check out await-sync HOT 2
- Compliance issue in sync-request HOT 2
- Error: self signed certificate HOT 1
- on nodejs 20.10.0, Uncaught Error Error: connect ECONNREFUSED 127.0.0.1:49792
- Regarding the packaging error of using esbuild
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from sync-request.