2.0.0 • Published 5 years ago

react-http-request v2.0.0

Weekly downloads
90
License
MIT
Repository
github
Last release
5 years ago

React-http-request

npm npm

React component exposes network request functionality

Useful component to perform a network request and parse the response using superagent module.

Installation

Using npm:

npm install --save react-http-request

Supposing a CommonJS environment, you can simply use the component in this way:

import React, { Component } from 'react';
import Request from 'react-http-request';

export default class App extends Component {
  render() {
    return (
      <Request
        url='https://api.github.com/users/mbasso'
        method='get'
        accept='application/json'
        verbose={true}
      >
        {
          ({error, result, loading}) => {
            if (loading) {
              return <div>loading...</div>;
            } else {
              return <div>{ JSON.stringify(result) }</div>;
            }
          }
        }
      </Request>
    );
  }
}

Documentation

This component use its props to perform a network request based on superagent. Here you can find all details about usage.

Callback

The prop children of this component would be a function that takes an object as parameter. This object is composed by 3 properties:

PropertyDescription
errorContains superagent request error, see this for more information
resultContains superagent request response, see this for more information
loadingTrue or false, indicates if request is loading or is finished

This function will be triggered the first time immediately and second time when request is complete. You can see an example to clarify this concept in Installation section.

Props

Here is the list of the props used by the component, as we said before, it is based on superagent, so, you will find the complete documentation in its site, you will find a link for most props.

PropertyTypeDescription
urlStringRequest url
methodStringRequest method: get, head, post, put, delete
childrenfunction({ error, result, loading }): return nodeFunction explained above in "callback" section
typeStringHeader Content-Type. docs
acceptStringHeader Accept. docs
timeoutNumberA timeout after which an error will be triggered. docs
verboseBooleanIf true, error messages will automatically be logged to the console
queryObject or StringQuery parameter. docs
sendObject or StringPost parameter. docs
headersObjectRequest header. docs
auth{ user: '', pass: '' }Basic authentication. docs
withCredentialsBooleanEnables the ability to send cookies from the origin, however only when "Access-Control-Allow-Origin" is not a wildcard ("*"), and "Access-Control-Allow-Credentials" is "true". docs
bufferBooleanTo force buffering of response bodies as result.text. docs
attachArray of { name: '', path: '', filename: '' }Attach files. docs
fieldsArray of { name: '', value: '' }Much like form fields in HTML, you can set field values. docs
onRequestfunction(request): return requestFunction that will be called before sending the request. It accept as parameter the request itself and must return the request. This means that you can manually modify the request that will be sent.

Author

Matteo Basso

Copyright and License

Copyright (c) 2016, Matteo Basso.

React-http-request source code is licensed under the MIT License.