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

Problem with POST #94

Closed
matiux opened this issue Aug 26, 2015 · 2 comments
Closed

Problem with POST #94

matiux opened this issue Aug 26, 2015 · 2 comments

Comments

@matiux
Copy link

matiux commented Aug 26, 2015

This is my code:

            var opts = {

                method: 'POST',
                encoding: 'utf8',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: {
                    "email": "my@mail.com",
                    "password": "mypassword"
                }
            }

            got(base_url + '/url_to_post', opts, function(err, data, res) {

                expect(res.statusCode).toBe(200);

                done();

            });

I have a problem with send jason data because in node server I get this error:

Error: invalid json
at parse (...../node_modules/body-parser/lib/types/json.js:84:15)
.....

If I try to print the body in ...../node_modules/body-parser/lib/types/json.js

.....
  function parse(body) {
    console.log(body);
    if (body.length === 0) {
      // special-case empty json body, as it's a common client-side mistake
      // TODO: maybe make this configurable or part of "strict" option
      return {}
    }
.....

I get this log:

email=my%40mail.com&password=mypassword

Some ideas?

@matiux
Copy link
Author

matiux commented Aug 26, 2015

I have solved!!

            var opts = {

                method: 'POST',
                encoding: 'utf8',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify(body)

            }

@floatdrop
Copy link
Contributor

Object in body is querystringified, not JSON-stringified (it is quite confusing, but that's how things go for now). Glad, that you make it :)

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

No branches or pull requests

2 participants