How can I make a POST request from a Protractor test?

You can just use another library to run the POST request if you just want to populate your database.

For example, you can use superagent in your beforeEach like so:

var request = require( "superagent" );

describe( "Something", function() {

  beforeEach( function( done ) {
    request
      .post( "http://localhost/api/foo" )
      .send( {data : "something"} )
      .end( done );
  } );

} );

I found a way to do it, with the help of Andres D. The gist of it is to run a script in the browser via browser.executeAsyncScript and inject the $http service in there. The $http service is then told to make a POST request. Here's example CoffeeScript of how it's done:

browser.get('http://your-angular-app.com')
browser.executeAsyncScript((callback) ->
  $http = angular.injector(["ng"]).get("$http")
  $http(
    url: "http://yourservice.com"
    method: "post"
    data: yourData
    dataType: "json"
  )
  .success(->
    callback([true])
  ).error((data, status) ->
    callback([false, data, status])
  )
)
.then((data) ->
  [success, response] = data
  if success
    console.log("Browser async finished without errors")
  else
    console.log("Browser async finished with errors", response)
)

It is possible to run some async setup code in your onPrepare function of your protractor config. You need to explicitly tell protractor to wait for your request to finish. This can be done with flow.await() which plays nice with promises.

onPrepare: function() {

  flow = protractor.promise.controlFlow()

  flow.await(setup_data({data: 'test'})).then( function(result) {
    console.log(result);
  })

}

** As of protractor 1.1.0 on prepare can return a promise, so the use of flow to explictly wait for the promise to resolve is unnecessary.

See: https://github.com/angular/protractor/blob/master/CHANGELOG.md