簡體   English   中英

如何在Flutter中向API服務器發送請求POST消息?

[英]How to send request POST message to API server in flutter?

我正在使用NAVER API來檢測人臉,因此必須將POST消息發送到API服務器。 消息的格式如下。

[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}--  

在檢查格式之后,我使用MultipartRequestMultipartFile編寫。

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);
}

但是此代碼得到401錯誤,這是UNAUTHORIZED 問題是什么? 我該如何解決?

X-Naver...值是HTTP 標頭 ,而不是表單字段 像這樣添加它們:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM