【问题标题】:BadRequestHttpException POST REST ResourceBadRequestHttpException POST REST 资源
【发布时间】:2018-05-18 04:31:21
【问题描述】:

我正在尝试创建自定义 REST 资源。我的模块如下:

/example.info.yml

name: Example
description: ''
type: module
core: 8.x
version: DEV
dependencies:
 - serialization
 - basic_auth
 - rest

/src/Plugin/rest/resource/BasicGetResource.php

namespace Drupal\Example\Plugin\rest\resource;

use Drupal\rest\Plugin\ResourceBase;
use Drupal\rest\ResourceResponse;

/**
 * Provides an Example Resource
 *
 * @RestResource(
 *   id = "example_resource",
 *   label = @Translation("Example Resource"),
 *   uri_paths = {
 *     "canonical" = "/api/basic",
 *     "https://www.drupal.org/link-relations/create" = "/api/basic"
 *   }
 * )
 */

class ExampleResource extends ResourceBase {
    /**
    * Responds to GET requests.
    * @return \Drupal\rest\ResourceResponse
    */
    public function get() {
        $response = ['data' => 'Basic Authentication'];
        return new ResourceResponse($response);
    }

    /**
    * Responds to POST requests.
    * @return \Drupal\rest\ResourceResponse
    */
    public function post($data) {
        $response = ['data' => $data];
        return new ResourceResponse($response);
    }
}

/config/install/rest.resource.example_resource.yml

langcode: en
status: true
dependencies:
  module:
    - basic_auth
    - example
    - serialization
_core:
  default_config_hash: NSa-WWwv2X-ogB4ojX2-m6rCsWMY6tzOZFZySnI5yfM
id: example_resource
plugin_id: example_resource
granularity: resource
configuration:
  methods:
    - GET
    - POST
  formats:
    - json
  authentication:
    - basic_auth

将以下 Javascript 附加到块以进行 POST:

var token;
var credentials = btoa("[USERNAME]:[PASSWORD]");

var settings = {
    "async": true,
    "crossDomain": true,
    "url": "/rest/session/token",
    "method": "GET",
    "headers": {"Content-Type": "application/x-www-form-urlencoded"},
};

$.ajax(settings).done(function (response) {
    token = response;
});

$('#btn-basic-post').click(function() {
    var form = new FormData();
    form.append("message", "Hello World!");

    var settings = {
        "async": true,
        "crossDomain": true,
        "url": "/api/basic?_format=json",
        "method": "POST",
        "headers": {
                "x-csrf-token": token,
            "authorization": "Basic " + credentials,
            "cache-control": "no-cache",
            "Content-Type": "application/json",
            "Accept": 'application/json',
          },
          "processData": false,
          "contentType": false,
          "mimeType": "multipart/form-data",
          "data": form
        }

        $.ajax(settings).done(function (response) {
          alert(response.data);
        });

    });

我收到状态 400,响应为 {"message":"Syntax error"}。以下日志为记录:

Symfony\Component\HttpKernel\Exception\BadRequestHttpException: Syntax error in Drupal\rest\RequestHandler->handle() (line 101 of C:\Users\bamberj\Sites\devdesktop\drupal-rest\core\modules\rest\src\RequestHandler.php).

任何建议将不胜感激。谢谢。

https://www.drupal.org/project/drupal/issues/2928340

【问题讨论】:

标签: rest post drupal drupal-8 bad-request


【解决方案1】:

数据未正确编码...

var data = JSON.stringify({
    message: 'Hello World',
});

var settings = {
  "async": true,
  "crossDomain": true,
  "url": "/api/basic?_format=json",
  "method": "POST",
  "headers": {
    "x-csrf-token": token,
    "authorization": "Basic YWRtaW46YWRtaW4=",
    "cache-control": "no-cache",
    "Content-Type": "application/json",
    "Accept": 'application/json',
  },
  "data": data,
  "dataType": "JSON",
}

$.ajax(settings).done(function (response) {
  console.log(response);
});

谢谢 cilefen。见https://www.drupal.org/project/drupal/issues/2928340#comment-12371675

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-26
    • 2011-01-13
    • 1970-01-01
    • 1970-01-01
    • 2017-10-16
    • 1970-01-01
    • 2014-04-04
    相关资源
    最近更新 更多