gpt4 book ai didi

api - 如何在 flutter 中向 API 服务器发送请求 POST 消息?

转载 作者:IT王子 更新时间:2023-10-29 07:23:50 24 4
gpt4 key购买 nike

我正在使用 NAVER API 检测人脸,所以我必须向 API 服务器发送 POST 消息。消息格式如下。

[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 header ,而不是表单字段。而是像这样添加它们:

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

关于api - 如何在 flutter 中向 API 服务器发送请求 POST 消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54069031/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com