Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Response as string #2220

Open
cnrahulrkr opened this issue Aug 3, 2021 · 1 comment
Open

Response as string #2220

cnrahulrkr opened this issue Aug 3, 2021 · 1 comment

Comments

@cnrahulrkr
Copy link

cnrahulrkr commented Aug 3, 2021

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.

@cah-brian-gantzler
Copy link
Collaborator

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants