I build REST API with Spring Boot and I use this API in react app.
I would like to create a new resource(POST) and right after creating it I would like to add a file to this resource (PUT). I don't do it in one request because I follow that answer
There is the POST request:
fetch(`http://localhost:8080/authors`, {
method: 'post',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
},
body: JSON.stringify({
//body
})
}).then(r => console.log(r))
From the server's point of view, I handle this url like this:
//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);
}
So I return response with Location header. My question is: How can I use this to send another request to the location this header points to ?
As you seen above I used console.log
to see what server give me as a response. This is it:
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"
There is no information about the Location
header, so I don't know how to use it as the response doesn't contain it