如何发送文件和json,使用fetch反应原生

时间:2017-05-03 08:17:13

标签: javascript json react-native

我需要从ReactNative js向服务器发送请求到这个结果我该怎么办?

Parameters: {"user"=>{"username"=>"test", "email"=>"test@test.test", "password"=>"[FILTERED]", "repeat_password"=>"[FILTERED]", "telephone"=>"123456789", "location"=>"bla bla blalaldlval", "avatar"=>#<ActionDispatch::Http::UploadedFile:0x007f76fd47b910 @tempfile=#<Tempfile:/tmp/RackMultipart20170503-4-k6vif.jpg>, @original_filename="01858.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"user[avatar]\"; filename=\"01858.jpg\"\r\nContent-Type: image/jpeg\r\n">}}

我试试

static async createUser(paramsUser) {
  let response = await fetch('http://test.herokuapp.com/users', {
    method: 'POST',
    headers: {
     'Accept': 'application/json',
    'Content-Type': 'application/json'
    },
    body: JSON.stringify({
     user: {
      username: paramsUser.username,
      email: paramsUser.email,
      password: paramsUser.password,
      telephone: paramsUser.telephone,
      location: paramsUser.location,
      avatar: paramsUser.avatar, // this is image
     }
    })
  });
  return response = JSON.parse(await response.text())
}

但他唯一的json以及如何发送文件?

0 个答案:

没有答案