Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pass headers to prepareBody #134

Merged
merged 1 commit into from
Jan 10, 2016
Merged

pass headers to prepareBody #134

merged 1 commit into from
Jan 10, 2016

Conversation

bekzod
Copy link
Contributor

@bekzod bekzod commented Dec 14, 2015

passing headers to prepareBody can be useful when you want to serialize depending content-type header, for example if it is text/...

@DingoEatingFuzz
Copy link
Contributor

Why do this when there is a prepareHeaders method?

Are you needing the headers to make decisions regarding the body?

@bekzod
Copy link
Contributor Author

bekzod commented Dec 15, 2015

@DingoEatingFuzz you mean "why not to do this inside prepareHeaders" ?
well, there you don't have access to body
you mean "Do you need headers to make decisions regarding body?"
yes

   this.get('/api/songs', function(request){
    return [
      200,
      {'content-type': 'text/csv'},
      "firstname,lastname\nbob,dylon"
    ];
  });

and if I have prepareBody like this

server.prepareBody = function(body){
  return body ? JSON.stringify(body) : '{"error": "not found"}';
}

then I get ""firstname,lastname\nbob,dylon"" double quotes as the header is text/csv

so I would like to do this to fix it

server.prepareBody = function(body, headers) {
  if (headers['content-type'] === 'application/javascript') {
    return body ? JSON.stringify(body) : '{"error": "not found"}';
  } else {
    return body;
  }
}

@trek
Copy link
Member

trek commented Jan 10, 2016

Thanks!

trek added a commit that referenced this pull request Jan 10, 2016
pass headers to prepareBody
@trek trek merged commit 32607c5 into pretenderjs:master Jan 10, 2016
@trek
Copy link
Member

trek commented Jan 10, 2016

This is now released in 0.12.0

@bekzod bekzod deleted the pass-headers branch January 10, 2016 15:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants