在POST请求错误422角2的Drupal 8

在POST请求错误422角2的Drupal 8

问题描述:

我试图发送一个POST请求到Drupal的网站(创建内容类型的测试,一个新的节点),但我得到错误422(无法处理的实体)在POST请求错误422角2的Drupal 8

这是我在角服务:

createBlog(blog: Blog): Observable<any>{ 
    let url = this.API_URL + "entity/node"; 
    blog._links = {type : { href: 'http://example.co.uk/rest/type/node/test' } }; 
    return this.http.post(url, blog, {headers:this.headers}).map(res => res.json()).catch(err => { 
console.log(blog) 
    return Observable.throw(err.json); 
    }); 
} 

这是我所得到的控制台表单提交后:

enter image description here

任何想法?

使用JSON.stringify(data)方法在通过http发布之前。

return this.http.post(url, JSON.stringify(blog), headers).map((res: Response) => res.json()); 

编辑:

阿苏使用头状的下方。

import { Headers, Response, RequestOptions} from "@angular/http"; 
... 

let headers = new Headers({ 'Content-Type': 'application/json' }); 
let options = new RequestOptions({ headers: headers }); 
let body = JSON.stringify(blog); 
return this.http.post(url, body, options).map((res: Response) => res.json()); 
+0

我试过,但没有成功......同样的错误 –

+0

检查我的编辑可能是头语法不正确。 – Satendra

+0

仍然不起作用。对邮差工作起来很不错。 –

对于此功能:

addTask (task: Task): Observable<Task> { 

    const url = `${this.mainUrl}/entity/node`; 

    const postReturn = this.http.post(url, task, httpHaljson); 

    return postReturn 
} 

我这是怎么声明API和头:

mainUrl : 'http://drupal.dd:8083', 
    httpHaljson : { 
        headers: new HttpHeaders({ 
        "X-CSRF-Token": "Qfnczb1SUnvOAsEy0A_xuGp_rkompgO2oTkCBOSEItM", 
        "Authorization": "Basic Qfnczb1SUnvOAsEy0A_xuGp_rkompgO2oTkCBOSEItM", // encoded user/pass - this is admin/123qwe 
        // "Content-Type": "application/json" 
        "Content-Type": "application/hal+json" 
        }) 
       } 

和任务,最重要的一条,要像做这个:

{ 
    "_links": { 
    "type": { 
     "href": "http://drupal.dd:8083/rest/type/node/task" 
    } 
    }, 
    "title": { 
    "value": "I am a new task" 
    }, 
    "type": { 
    "target_id": "task" 
    } 
} 

为此,在从表单recive一些数据,我这样做:

onSubmit(name: string, body:string): void { 
    let task: any = { 
     _links: null, 
     type: null, 
     title: null, 
     body: null  
    }; 

    task._links = {type: {"href": "http://drupal.dd:8083/rest/type/node/task"} }; 
    task.type = {target_id: "task"}; 
    task.title = {value: name}; 
    task.body = { "": body}; 

     this.taskService.addTask(task) 
     .subscribe(task => { 
      this.tasks.push(task); 
      // console.log(JSON.stringify(task)); 

      this.getTasks(); 
     }); 
    } 

看我:执行console.log(JSON.stringify(任务));是关键,有了这个,你可以看到你创建drupal需要多少东西。

看看这个guide