是否可以 POST 请求 responseType "array buffer"?
Is it posible to POST request the responseType "array buffer"?
我制作了一个 api 将 return 图像。起初我在 get 方法请求上尝试了它并且它有效,但出于安全原因我需要使用 post 方法但 post 不像我的 get 方法那样工作。您是否认为 responseType 仅在 get 方法中可用,因为它不适用于我的 post 方法?
这是我使用有效的 get 方法的代码:
前端:
export const getImageFile = async (imageName) => {
try {
const { data } = await axios.get(`${apiUrl}/image/${imageName}`,{
responseType: "arraybuffer",
});
const image = new Buffer.from(data, "binary").toString("base64");
return `data:image/png;base64, ${image}`;
} catch (err) {
alert("File not found.");
}
};
后端(php symfony):
/**
* @Route("/api/graph/image/{imageName}", methods={"GET"}, name="get- image-graph")
*/
public function getImage($imageName)
{
try {
$file = $this->parameterBag->get('kernel.project_dir').'/public/graphImage/graphs/'.$imageName;
$response = new BinaryFileResponse($file);
return $response;
} catch (\Exception $e) {
return $this->json(['error' => 'File not found'], JsonResponse::HTTP_NOT_FOUND);
}
}
这是我的代码,当我使用 POST 方法时不起作用:
前端:
export const getImageFile = async (imageName) => {
try {
const { data } = await axios.post(`${apiUrl}/image`,{
responseType: "arraybuffer",
"imageName": imageName,
});
const image = new Buffer.from(data, "binary").toString("base64");
return `data:image/png;base64, ${image}`;
} catch (err) {
alert("File not found.");
}
};`
backend:
```php
/**
* @Route("/api/graph/image", methods={"POST"}, name="get-image-
graph")
*/
public function getImage(Request $request)
{
try {
$content = json_decode($request->getContent(), true);
$imageName = $content["imageName"];
$file = $this->parameterBag->get('kernel.project_dir')
.'/public/graphImage/graphs/'.$imageName;
$response = new BinaryFileResponse($file);
return $response;
} catch (\Exception $e) {
return $this->json(['error' => 'File not found'],
JsonResponse::HTTP_NOT_FOUND);
}
}`
axios.post(url, data, config)
您的post请求:
const { data } = await axios.post(`${apiUrl}/image`,{
responseType: "arraybuffer",
"imageName": imageName,
});
您似乎混合了数据和配置。所以你的 post 请求应该是
const {data} = await axios.post(
`${apiUrl}/image`,
{
"imageName": imageName,
},
{
responseType: "arraybuffer"
}
);
// In server side php,you access it like
$image = $_POST["imageName"];
如果您想将其作为 json 发送,请在配置
中使用 headers
{
responseType: "arraybuffer",
headers: {'Content-Type': 'application/json'}
}
我制作了一个 api 将 return 图像。起初我在 get 方法请求上尝试了它并且它有效,但出于安全原因我需要使用 post 方法但 post 不像我的 get 方法那样工作。您是否认为 responseType 仅在 get 方法中可用,因为它不适用于我的 post 方法?
这是我使用有效的 get 方法的代码:
前端:
export const getImageFile = async (imageName) => {
try {
const { data } = await axios.get(`${apiUrl}/image/${imageName}`,{
responseType: "arraybuffer",
});
const image = new Buffer.from(data, "binary").toString("base64");
return `data:image/png;base64, ${image}`;
} catch (err) {
alert("File not found.");
}
};
后端(php symfony):
/**
* @Route("/api/graph/image/{imageName}", methods={"GET"}, name="get- image-graph")
*/
public function getImage($imageName)
{
try {
$file = $this->parameterBag->get('kernel.project_dir').'/public/graphImage/graphs/'.$imageName;
$response = new BinaryFileResponse($file);
return $response;
} catch (\Exception $e) {
return $this->json(['error' => 'File not found'], JsonResponse::HTTP_NOT_FOUND);
}
}
这是我的代码,当我使用 POST 方法时不起作用:
前端:
export const getImageFile = async (imageName) => {
try {
const { data } = await axios.post(`${apiUrl}/image`,{
responseType: "arraybuffer",
"imageName": imageName,
});
const image = new Buffer.from(data, "binary").toString("base64");
return `data:image/png;base64, ${image}`;
} catch (err) {
alert("File not found.");
}
};`
backend:
```php
/**
* @Route("/api/graph/image", methods={"POST"}, name="get-image-
graph")
*/
public function getImage(Request $request)
{
try {
$content = json_decode($request->getContent(), true);
$imageName = $content["imageName"];
$file = $this->parameterBag->get('kernel.project_dir')
.'/public/graphImage/graphs/'.$imageName;
$response = new BinaryFileResponse($file);
return $response;
} catch (\Exception $e) {
return $this->json(['error' => 'File not found'],
JsonResponse::HTTP_NOT_FOUND);
}
}`
axios.post(url, data, config)
您的post请求:
const { data } = await axios.post(`${apiUrl}/image`,{
responseType: "arraybuffer",
"imageName": imageName,
});
您似乎混合了数据和配置。所以你的 post 请求应该是
const {data} = await axios.post(
`${apiUrl}/image`,
{
"imageName": imageName,
},
{
responseType: "arraybuffer"
}
);
// In server side php,you access it like
$image = $_POST["imageName"];
如果您想将其作为 json 发送,请在配置
中使用 headers {
responseType: "arraybuffer",
headers: {'Content-Type': 'application/json'}
}