When retrieving an image from a REST API through an HTTP GET with a request body, I have successfully verified the returned content using node.js
and chai.js
:
expect(res).to.have.header('Content-Type', 'image/jpeg');
expect(res).to.have.header('Access-Control-Allow-Origin', '*');
expect(res).to.have.header('Access-Control-Allow-Headers', 'Access-Control-Allow-Headers, Origin, X-Requested-With, Content-Type, Accept, Authorization');
expect(res).to.have.header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS, HEAD');
expect(res).to.have.status(200);
expect(res.body).to.be.instanceof(Buffer); // the image content
In my vue.js
file, I typically attach an image to an <img ...>
HTML tag as follows:
<img v-bind:src="urlImg">
I specify the URL in the JavaScript part like this:
# The URL string is returned by a function
this.urlImg = 'http://example.com/my_img.jpeg'
However, in this scenario, providing the URL is not possible because the HTTP GET expects a body to return the image with content type image/jpeg
.
I am unsure if this is feasible and if my understanding of the content type image/jpeg
is correct. How can I accomplish this in vue.js
? Is it even doable? Is there a way to check the image content of the HTTP response similar to tools like Postman (Chrome app) which cannot inspect the response pretending to treat it as text/JSON.
EDIT
Regarding the accepted answer: the last solution proposed (UPDATE 2) worked for me (using HTTP POST with a JSON body for the request). Ensure you utilize axios
(https://github.com/axios/axios) for performing the HTTP requests (you can import it in the <script>
section of the Vue file like this: import axios from "axios";
).
I initially used vue-resource
(https://github.com/pagekit/vue-resource) thinking it was similar to axios
, but it took some time for me to realize that they are different.