我使用 Spring Boot构建REST API ,并在反应应用程序中使用此 API。
我想创建一个新资源(POST),并在创建它之后立即向该资源(PUT)添加一个文件。我不会在一个请求中这样做,因为我遵循那个答案
有POST请求:
fetch(`http://localhost:8080/authors`, {
method: 'post',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
},
body: JSON.stringify({
//body
})
}).then(r => console.log(r))
从服务器的角度来看,我这样处理这个 url:
//URL: http://localhost:8080/authors
@PostMapping
ResponseEntity<Author> createAuthor(@RequestBody Author author) {
var result = repository.save(author); //JpaRepository
return ResponseEntity.created(URI.create("/" + result.getAuthorId())).body(result);
}
所以我用 Location 头返回响应。我的问题是:如何使用它向此标头指向的位置发送另一个请求?正如你在上面看到的,我曾经console.log看到什么服务器给我作为响应。就是这个:
Response { type: "cors", url: "http://localhost:8080/authors", redirected: false, status: 201, ok: true, statusText: "", headers: Headers, body: ReadableStream, bodyUsed: false }
body: ReadableStream { locked: false }
bodyUsed: false
headers: Headers { }
<prototype>: HeadersPrototype { append: append(), delete: delete(), get: get(), … }
append: function append()
constructor: function ()
delete: function delete()
entries: function entries()
forEach: function forEach()
get: function get()
has: function has()
keys: function keys()
set: function set()
values: function values()
Symbol(Symbol.toStringTag): "Headers"
Symbol(Symbol.iterator): function entries()
<prototype>: Object { … }
ok: true
redirected: false
status: 201
statusText: ""
type: "cors"
url: "http://localhost:8080/authors"
没有关于Location标头的信息,所以我不知道如何使用它,因为响应不包含它