Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I want to add header with access_token for flutter upload image

My API requirement is

URL: /user/upload-profile-image

method= POST

header--

Accesstoken: "access_token"

content-type = multipart/form-data

This is my code:

Future getUploadImg(File _image) async {

  String apiUrl = '$_apiUrl/user/upload-profile-image';

  final length = await _image.length();

  final request = new http.MultipartRequest('POST', Uri.parse(apiUrl))
      ..files.add(new http.MultipartFile('avatar', _image.openRead(), length));

  http.Response response = await http.Response.fromStream(await request.send());

  print("Result: ${response.body}");

  return JSON.decode(response.body);

}
like image 507
Rahul Mishra Avatar asked Aug 24 '18 13:08

Rahul Mishra


People also ask

How do you add a header on flutter?

In the Flutter Event Calendar, you can customize the header and view header and it can be achieved by hiding headers and placing Container, Row, and Column widgets of the flutter. STEP 1: Set the `HeaderHeight` and `ViewHeaderHeight` properties to `0` to hide the default headers.

How do I upload images using HTTP flutter?

I have to use the basename() function to get the image file path. That image file is memory image file.To use that function you need to import the path package which I have mentioned before. Add this into your button function. After you select image hit the upload button.

How to upload images to the server using flutter?

It is commonly used by HTTP clients to upload files to the Server. In this example, first, we choose the image from the gallery using ImagePicker and then upload images to the server using PHP. First, add the image_picker Flutter package as a dependency by adding the following line in your pubspec.yaml file.

How to add images to assets folder in flutter?

a) To add images, write the following code: flutter: assets: - assets/images/yourFirstImage.jpg - assets/image/yourSecondImage.jpg. b) If you want to include all the images of the assets folder then add this: flutter: assets: - assets/images/. Note: Take care of the indentation, assets should be properly indented to avoid any error.

How to pick image from Gallery in flutter?

First, add the image_picker Flutter package as a dependency by adding the following line in your pubspec.yaml file. Now write the function for pick image from Gallery. The optional parameter imageQuality accepts any value between 0 to 100, you can adjust it according to the size and quality required by your app.

What are assets in flutter?

Common types of assets include static data (for example, JSON files), configuration files, icons, and images (JPEG, WebP, GIF, animated WebP/GIF, PNG, BMP, and WBMP). Flutter uses the pubspec.yaml file, located at the root of your project, to identify assets required by an app.


2 Answers

Can you try to add headers like below

Map<String, String> headers = { "Accesstoken": "access_token"};

final multipartRequest = new http.MultipartRequest('POST', Uri.parse(apiUrl))
multipartRequest.headers.addAll(headers);
multipartRequest.files.add(..)
like image 106
Dinesh Balasubramanian Avatar answered Sep 20 '22 21:09

Dinesh Balasubramanian


  var request = http.MultipartRequest(
    "POST",
    Uri.parse(
      "${Urls().url}/support/tenant/register",
    ),
  );
  //add text fields
  
  request.headers["authorization"]=userToken;

  request.fields["type"] = type;
  request.fields["note"] = note;
  for (var item in path) {
    var ext = item.split('.').last;
    var pic = await http.MultipartFile.fromPath("images", item, contentType: MediaType('image', ext));
    request.files.add(pic);
  }

  //add multipart to request

  var response = await request.send();
  var responseData = await response.stream.toBytes();
  var responseString = String.fromCharCodes(responseData);

  var d = jsonDecode(responseString);
like image 22
Dipanjan Panja Avatar answered Sep 18 '22 21:09

Dipanjan Panja