Giter VIP home page Giter VIP logo

Comments (9)

ForbesLindesay avatar ForbesLindesay commented on September 10, 2024

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.

paraplexed avatar paraplexed commented on September 10, 2024

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.

ForbesLindesay avatar ForbesLindesay commented on September 10, 2024

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.

paraplexed avatar paraplexed commented on September 10, 2024

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.

ForbesLindesay avatar ForbesLindesay commented on September 10, 2024

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.

sbancal avatar sbancal commented on September 10, 2024

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.

ForbesLindesay avatar ForbesLindesay commented on September 10, 2024

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.

sbancal avatar sbancal commented on September 10, 2024

Great, that was it! Thanks 👍

from sync-request.

adiGitUser avatar adiGitUser commented on September 10, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.