baeharam
baeharam

Reputation: 543

How to send request POST message to API server in flutter?

I'm using NAVER API to detect faces, so I have to send POST message to API server. The format of message is like below.

[HTTP Request Header]
POST /v1/vision/face HTTP/1.1  
Host: openapi.naver.com  
Content-Type: multipart/form-data; boundary={boundary-text}  
X-Naver-Client-Id: {Client ID}
X-Naver-Client-Secret: {Client Secret}  
Content-Length: 96703  

--{boundary-text}  
Content-Disposition: form-data; name="image"; filename="test.jpg"  
Content-Type: image/jpeg  

{image binary data}  
--{boundary-text}--  

After I checked format, I wrote using MultipartRequest and MultipartFile.

Future<void> getFaceData() async {
  final Uri url = Uri.parse('https://openapi.naver.com/v1/vision/face');
  final request = http.MultipartRequest('POST',url);
  request.fields['X-Naver-Client-Id'] = 'client key(I added real value)';
  request.fields['X-Naver-Client-Secret'] = 'client secret(I added real value)';
  request.files.add(await http.MultipartFile.fromPath(
    'image',
    _image.path,
    contentType: MediaType('multipart','form-data')
  ));

  http.StreamedResponse response = await request.send();
  print(response.statusCode);
}

But this code gets 401 error which is UNAUTHORIZED. What is the problem? How can I fix it?

Upvotes: 0

Views: 613

Answers (1)

Richard Heap
Richard Heap

Reputation: 51682

The X-Naver... values are HTTP headers, rather than form fields. Add them like this instead:

 request.headers['X-Naver-Client-Id'] = 'client key(I added real value)';
 request.headers['X-Naver-Client-Secret'] = 'client secret(I added real value)';

Upvotes: 1

Related Questions