2

ReactNative provide me with fetch to send a httpRequest.The attribute of body includes my parameters which are to send to the server.But I can't get the parameters on my server.My codes are here:

fetch(`${keys.api}/login`, 
        {
            method: 'POST',
            body: JSON.stringify({
                username: this.state.username,
                password: this.state.password,
            }),
            headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/json',
            }, 

        }
    ).then((response) => {
        if(response._bodyText == 'success') {
            this.props.resetToRoute({
                name: '主页',
                component: Main,
                hideNavigationBar: true,
            });
        } else {
            this.cancelLogin();
        }
    }).catch((error) => {
        console.warn(error);
        this.cancelLogin();
    });

And the console in my J2EE Web Server prints the message: The httpRequest message

There is no parameter in my httpRequest(In other words,The body can not deliver any parameters),I need help. It's dangerous to show my username and password in the url.

blank
  • 29
  • 5

1 Answers1

-1

i have met the problem twice on jetty-8.1 on different condition
first ,you should know that it has nothing to do with react-native
fetch put the data in body to header "payload" when the client made a request.i thought jetty-8.1 does not support get data from the payload header ,change the way Getting request payload from POST request in Java servlet will be helpful
or maybe use the websockt or XMLhttpRequest object to send a request

// Read from request
StringBuilder buffer = new StringBuilder();
BufferedReader reader = request.getReader();
String line;
while ((line = reader.readLine()) != null) {
    buffer.append(line);
}
String data = buffer.toString()
Community
  • 1
  • 1
xcchcaptain
  • 113
  • 3
  • 14