【问题标题】:Simple API Call gets redirected简单 API 调用被重定向
【发布时间】:2021-01-18 13:07:29
【问题描述】:

我正在创建一个 Nextcloud-App(使用 Nextcloud 20)。我想简单地调用我的外部服务。由于 CSP 限制(由 netxcloud 默认设置),我根本做不到。

每次我使用 window.OC.generateUrl('/apps/[MYAPP]/video/trim'); 请求我的 URL 时,我都会收到重定向响应(代码 302),而不是成功(代码 200)。我错过了什么?

我注册了我的路线:

// [MYAPP]/appinfo/routes.php
return [
    'routes' => [
       ['name' => 'video#trim', 'url' => '/trim', 'verb' => 'POST']
    ]
];

我已经构建了我的控制器:

// [MYAPP]/lib/controller/VideoController.php
namespace OCA\[MYAPP]\Controller;

use OCP\IRequest;
use GuzzleHttp\Client;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\DataResponse;

class VideoController extends Controller
{

   /**
    * @NoAdminRequired
    */
    public function trim(string $title, string $content)
    {
        $client = new Client([
          'base_uri' => 'http://localhost:3000/push',
          'timeout'  => 2.0,
        ]);

        $response = $client->request('POST', ['json' => $content]);

        return new DataResponse(['foo' => 'bar']);
    }
}

我是 POSTing 我的请求。在控制台中,我看到重定向到 Location http://localhost:9000/apps/dashboard/

// js
const opts = {}; // my payload
const url = window.OC.generateUrl('/apps/[MYAPP]/video/trim');
fetch(url, {method: 'post', body: JSON.stringify(opts)})
    .catch((err) => console.error(err))
    .then((response) => response.json())
    .then((data) => console.log(data));

【问题讨论】:

    标签: nextcloud


    【解决方案1】:

    终于在routes.php中找到了问题!

    由于我正在为 /apps/[MYAPP]/video/trim 生成 URL,因此 routes.php 中的 url 应该看起来像 /video/trim 而不是 /trim

    // [MYAPP]/appinfo/routes.php
    return [
        'routes' => [
           ['name' => 'video#trim', 'url' => '/video/trim', 'verb' => 'POST']
        ]
    ];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-01-01
      • 2013-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-17
      • 1970-01-01
      相关资源
      最近更新 更多