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

[Network] Android throws up a redbox when POST has no body #3371

Closed
ide opened this issue Oct 12, 2015 · 14 comments
Closed

[Network] Android throws up a redbox when POST has no body #3371

ide opened this issue Oct 12, 2015 · 14 comments
Labels
Good first issue Interested in collaborating? Take a stab at fixing one of these issues. Resolution: Locked This issue was locked by the bot.

Comments

@ide
Copy link
Contributor

ide commented Oct 12, 2015

This occurs when you make a call like fetch(url, { method: 'post' }). The native layer should fill in an empty body instead of fataling.

@ide ide added the Android label Oct 12, 2015
@javache javache added the Good first issue Interested in collaborating? Take a stab at fixing one of these issues. label Oct 13, 2015
@javache
Copy link
Member

javache commented Oct 13, 2015

The error is being generated by OkHTTP, so we should change this in the call site.

@ide
Copy link
Contributor Author

ide commented Oct 13, 2015

I looked into this and OkHttp doesn't make it very easy to fix the caller since the HttpMethods class is internal and that's what they use to determine whether to require a body (or require no body). Changing the caller does look like the right fix though -- three ideas are:

  1. Reimplement the logic of HttpMethods
  2. Use try-catch in the caller
  3. Leave NetworkingModule alone and make Catalyst robust so it can handle the redbox and keep on going. Then adjust the XHR polyfill in JS to pass in a body when we think it's a POST.

@atticoos
Copy link
Contributor

Their required RequestBody stuff is such a pain. I had issues with the lack of support to allow DELETE with RequestBody in the past.

Polyfilling the body doesn't sound like a bad idea. Will the error subside if we supply a body argument with an empty string? The issue sounds to primarily exist due to the absence of a request body, versus it being empty, as described in square/okhttp#751. And it doesn't sound like there will be support in the library based on the HTTP spec requiring a request body.

@ide
Copy link
Contributor Author

ide commented Oct 14, 2015

I prefer option 3 in the list above. It's so much better when the native layer is robust and doesn't throw assertions when JS send it unexpected commands, and we also want to make the JS API (XHR polyfill in this case) convenient to use.

@atticoos
Copy link
Contributor

Yeah, very valid. The underlying system shouldn't fail for this unexpected input. So it sounds like this should be twofold --

  1. Improve Catalyst to not cripple by this unexpected input
  2. Add convenience to reduce user error by supplying an empty (rather than undefined) request body.

As a follow-on, it may be good to look at this behavior for other requests that may expect a request body, like PATCH or PUT.

@ide
Copy link
Contributor Author

ide commented Oct 14, 2015

This is the logic that OkHttp uses https://github.com/square/okhttp/blob/master/okhttp/src/main/java/com/squareup/okhttp/internal/http/HttpMethod.java#L27

I wish they made this part of their public API so we wouldn't have to replicate it but it's very simple code fortunately.

@atticoos
Copy link
Contributor

Yeahhh, i ran into this when debugging the DELETE stuff a while ago. Given that there are these 5 enforcements, I think that when addressing this we may want to cover at least those defined in the HTTP spec, POST, PUT, and PATCH when addressing the polyfill (not just POST alone). Thoughts?

@ide
Copy link
Contributor Author

ide commented Oct 14, 2015

We should handle the other HTTP methods too. I don't feel strongly about the PROPPATCH and REPORT methods but they're simple to support so why not?

@atticoos
Copy link
Contributor

On second thought, it would make sense to add polyfill support for those additional 2.
Since RN uses OkHTTP for its networking, and as the networking library requires post bodies for those two off-spec request methods, in the end there is more value in supporting those additional two. Otherwise we're just leaving two edges that are now known to fail based on the requiresRequestBody rule.

+1 on supporting all 5.

@PhilippKrone
Copy link
Contributor

Any update here? :) Perhaps @ide

@atom992
Copy link

atom992 commented Nov 2, 2015

the same error in HTTP PUT request.

@atticoos
Copy link
Contributor

Relatedly,

method patch must not have a request body

@qbig
Copy link
Contributor

qbig commented Dec 3, 2015

    if (data == null) {
      if(method.equals("POST")
              || method.equals("PUT")
              || method.equals("PATCH")){
        requestBuilder.method(method, RequestBody.create(null, new byte[0]));
      } else {
        requestBuilder.method(method, null);
      }
    }

@ide this would do?

@qbig
Copy link
Contributor

qbig commented Dec 3, 2015

basically copy-pasted the method 'HttpMethod.requiresRequestBody' from https://github.com/square/okhttp/blob/master/okhttp/src/main/java/com/squareup/okhttp/internal/http/HttpMethod.java#L27

This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Good first issue Interested in collaborating? Take a stab at fixing one of these issues. Resolution: Locked This issue was locked by the bot.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants
@javache @ide @atticoos @atom992 @qbig @PhilippKrone @react-native-bot and others