miragejs/ember-cli-mirage

Response as string

cnrahulrkr opened this issue · 1 comments

How can I response as string ?

I have an API which gives response as stringified JSON. At application run time this ajax API calls works perfectly. But, while test running, the API is getting response as JSON object not as string.

Mirage route code:

this.post("/api/products", function () {
   const productDetails = {
      ...
   }
   return new Mirage.Response(200, { }, JSON.stringify(productDetails));
});

Ajax API call code:

await ajax(`/api/products`, {
   type: "POST",
   contentType: "application/json",
   data: JSON.stringify(data)
})
.then(result => {
   // Actual: JSON object 
   // Expect: stringified JSON
   return result 
})

Can anyone could help ?

Edited:

Similar kind of approach, we required to have for an another API which expects buffer data instead of JSON object.

I think the problem might be that you are not setting a Content-Type header to say its a string. If you do not specify a content type, mirageJS defaults to json https://github.com/miragejs/miragejs/blob/master/lib/response.js#L44

If this question is still outstanding, could you close this issue and open a new issue in https://github.com/miragejs/miragejs/issues This functionality has been extracted to a new repo and no longer controlled by this addon.