【问题标题】:Angular 2 PUT content with uri/list content type(multiple elements)具有 uri/list 内容类型的 Angular 2 PUT 内容(多个元素)
【发布时间】:2018-09-10 05:21:34
【问题描述】:

如何在 Angular 2 打字稿中发送以下内容?

要发送多个 URI,我们必须用换行符分隔它们:

curl -i -X PUT -H "Content-Type:text/uri-list" --data-binary @uris.txt http://localhost:8080/authors/1/books

uris.txt 文件包含书籍的 URI,每个都在单独的行中:

http://localhost:8080/books/1 http://localhost:8080/books/2

我知道如何发送 PUT htt 请求,但没有找到在 PUT 请求中发送多个元素的解决方案!

我无法生成这样的内容: http://localhost:8080/books/1 http://localhost:8080/books/2

Typescript 总是将 "" 放在字符串中,我在服务器端遇到错误。

我可以这样发送:

 return this.http
    .put(`${this.apiUrl + this.quizesUrl}/${quiz.id}${this.questionsUrl}`
        , `${this.apiUrl + this.questionsUrl}/4`, QuizesService.OPTIONS_URI_LIST) // ...using put request
    .map(this.handleSingleResponse) // ...and calling .json() on the response to return data
    .catch(this.handleError) // ...errors if any
    .finally(() => {
      console.log('After updateQuestion request...');
    })

;

如何从一个集合中发送多个?

【问题讨论】:

  • 您是否尝试使用单个 PUT 请求更新多个实体?
  • 是的,测验有多个问题,我想在一个请求中分配所有问题。内容类型:uri/list 有这个能力。 baeldung.com/spring-data-rest-relationships

标签: angular spring-data-rest


【解决方案1】:
saveQuizQuestionAssignment(quiz: Quiz) {
  console.log(`${this.apiUrl + this.quizesUrl}/${quiz.id}${this.questionsUrl}`);
  console.log(this.createQuizQuestionRequest(quiz));
  return this.http
        .put(`${this.apiUrl + this.quizesUrl}/${quiz.id}${this.questionsUrl}`
            , this.createQuizQuestionRequest(quiz), QuizesService.OPTIONS_URI_LIST) 
        .map(this.handleSingleResponse) 
        .catch(this.handleError) 
        .finally(() => {
          console.log('After updateQuestion request...');
        }); 
}

createQuizQuestionRequest(quiz: Quiz) {
  var putRequestLine = '';
  quiz.questions.forEach(question => {    
    putRequestLine += `${this.apiUrl + this.questionsUrl + '/' + question.id}`+'\n';       
  });
  return putRequestLine;
}

我找到了非常简单的解决方案。 /当你疲于寻找最简单的事情时/

'createQuizQuestionRequest' 函数附加 uri-list 内容并被后端接受。然后请求将更新关系表(quiz_question)中的测验问题分配。

所以代码相当于postman中发送的以下请求:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-29
    • 2015-03-30
    • 1970-01-01
    • 1970-01-01
    • 2016-02-22
    • 1970-01-01
    相关资源
    最近更新 更多