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

r2.post not working vs fetch #73

Closed
DevBrent opened this issue Aug 20, 2018 · 1 comment
Closed

r2.post not working vs fetch #73

DevBrent opened this issue Aug 20, 2018 · 1 comment

Comments

@DevBrent
Copy link

DevBrent commented Aug 20, 2018

At my wits end trying to get this POST to return properly.

What else does r2 do to the request vs fetch? Something is wrong. I've tried using different initialization methods and alternate response management to try to get a working result.

Universal variables for all four examples:

const url = 'https://example.com/path';
const params = { example: 'request data', other_data: 'test' };

const body = Object.keys(params).map(key => encodeURIComponent(key) + '=' + encodeURIComponent(params[key])).join('&');

const headers = {
  'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
};

Broken, returns 400 error html:

const r2_example = await r2.post(url, {
  headers,
  body
});
console.log('r2', await r2_example.text);

Broken; alternate configuration, alternate response handling, still returning 400 error in html:

const r2_example_2 = await r2({
  url,
  body,
  method: 'POST',
  headers,
});
console.log('r2_2', await (await r2_example_2.response).text()); // 400 Bad Request

Working example with raw Header/Body

const fetch_example = await fetch(url, {
  method: 'POST',
  headers,
  body
});
console.log('fetch', await fetch_example.text());   // Proper result

Working example with node-fetch.Headers (globals created by r2) and Buffer.from(string).

const fetch_example_with_headers = await fetch(url, {
  method: 'POST',
  headers: new global.Headers(headers),
  body: Buffer.from(body)
});
console.log('fetch_example_with_headers', await fetch_example_with_headers.text()); // Proper result
@DevBrent
Copy link
Author

DevBrent commented Aug 20, 2018

I just confirmed replacing node-fetch 2.0.0-alpha.8 with 2.2.0 resolves this issue. In our other environments running yarn upgrade r2 resolved the issue as well even without merging #66

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

1 participant