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
responseType=text but json is parsed #2791
Comments
What you think should be the default content type returned in the case of this option enabled:
I'm asking, because in browser, for example, may be Response object which is the "raw" response, or any of these |
I am not familiar with browsers, but it looks like responseType=text should result in strings. For ArrayBuffer there's responseType=arraybuffer. |
did you try using axios |
you can see that from the code posted "to reproduce". |
oke, tested.. axios({
url: "http://localhost:1234/",
//responseType: "text",
transformResponse: []
}).then(function(res) {
console.log("Type of data is %s", typeof res.data);
}); that happens because of |
What do you think about this? const instance = axios.create({
baseURL: 'https://jsonplaceholder.typicode.com/',
transformResponse: (data) => JSON.stringify(data)
});
const fetchUserInStringType = async () => {
const response = await instance.get('/users/1')
console.log(response.data)
}
fetchUserInStringType() |
the main thought here, is why one option is choosen against another. should they depend on each other and stuff.. |
+1
works for me, but honestly, this is really bad...setting |
When JSON logs are returned this avoid decoding them. It's using a workaround from axios/axios#2791 (comment).
…be passed directly as payload for encoding to JSON #2613, #61, #907 (#3688) * Draft * Added support for primitive types to be converted to JSON if the request Content-Type is 'application/json'; Added throwing SyntaxError if JSON parsing failed and responseType is json; Added transitional option object; Added options validator to assert transitional options; Added transitional option `silentJSONParsing= true` for backward compatibility; Updated README.md; Updated typings; * Fixed isOlderVersion helper; Fixed typo; Added validator.spec.js; * Added forcedJSONParsing transitional option #2791 * `transformData` is now called in the default configuration context if the function context is not specified (for tests compatibility); * Added `transitional.clarifyTimeoutError` to throw ETIMEDOUT error instead of generic ECONNABORTED on request timeouts; Added support of onloadend handler if available instead of onreadystatechange; Added xhr timeout test; Fixed potential bug of xhr adapter with proper handling timeouts&errors (FakeXMLHTTPRequest failed to handle timeouts);
* Draft * Added support for primitive types to be converted to JSON if the request Content-Type is 'application/json'; Added throwing SyntaxError if JSON parsing failed and responseType is json; Added transitional option object; Added options validator to assert transitional options; Added transitional option `silentJSONParsing= true` for backward compatibility; Updated README.md; Updated typings; * Fixed isOlderVersion helper; Fixed typo; Added validator.spec.js; * Added forcedJSONParsing transitional option #2791 * `transformData` is now called in the default configuration context if the function context is not specified (for tests compatibility); * Added `transitional.clarifyTimeoutError` to throw ETIMEDOUT error instead of generic ECONNABORTED on request timeouts; Added support of onloadend handler if available instead of onreadystatechange; Added xhr timeout test; Fixed potential bug of xhr adapter with proper handling timeouts&errors (FakeXMLHTTPRequest failed to handle timeouts); * Removed unnecessary assertion;
Fixed with #3688 |
…be passed directly as payload for encoding to JSON axios#2613, axios#61, axios#907 (axios#3688) * Draft * Added support for primitive types to be converted to JSON if the request Content-Type is 'application/json'; Added throwing SyntaxError if JSON parsing failed and responseType is json; Added transitional option object; Added options validator to assert transitional options; Added transitional option `silentJSONParsing= true` for backward compatibility; Updated README.md; Updated typings; * Fixed isOlderVersion helper; Fixed typo; Added validator.spec.js; * Added forcedJSONParsing transitional option axios#2791 * `transformData` is now called in the default configuration context if the function context is not specified (for tests compatibility); * Added `transitional.clarifyTimeoutError` to throw ETIMEDOUT error instead of generic ECONNABORTED on request timeouts; Added support of onloadend handler if available instead of onreadystatechange; Added xhr timeout test; Fixed potential bug of xhr adapter with proper handling timeouts&errors (FakeXMLHTTPRequest failed to handle timeouts);
* Draft * Added support for primitive types to be converted to JSON if the request Content-Type is 'application/json'; Added throwing SyntaxError if JSON parsing failed and responseType is json; Added transitional option object; Added options validator to assert transitional options; Added transitional option `silentJSONParsing= true` for backward compatibility; Updated README.md; Updated typings; * Fixed isOlderVersion helper; Fixed typo; Added validator.spec.js; * Added forcedJSONParsing transitional option axios#2791 * `transformData` is now called in the default configuration context if the function context is not specified (for tests compatibility); * Added `transitional.clarifyTimeoutError` to throw ETIMEDOUT error instead of generic ECONNABORTED on request timeouts; Added support of onloadend handler if available instead of onreadystatechange; Added xhr timeout test; Fixed potential bug of xhr adapter with proper handling timeouts&errors (FakeXMLHTTPRequest failed to handle timeouts); * Removed unnecessary assertion;
Hello,
@jasonsaayman Are you talking about OP's issue ? Thanks |
Hi, is this behaviour expected? // standard
import fs from "fs";
import path from "path";
// external
import axios, { AxiosError } from "axios";
async function request() {
const url = "https://api.github.com";
const localFileName = path.join(__dirname, "response.txt");
try {
const response = await axios.get(url, {
responseType: "text",
transformResponse: [(data) => data],
});
fs.writeFile(localFileName, response.data, (fsError) => {
if (fsError) {
throw fsError;
}
});
} catch (error) {
if (error instanceof AxiosError) {
console.error(error.response.status, error.response.statusText);
} else {
console.error(error);
}
return null;
}
}
request(); JSON data gets written to a text file. I want to accept only files like at https://www.gnu.org/licenses/gpl-3.0.txt @liesauer workaround doesn't seem to work. |
Describe the bug
I want to get a response as string even if that comes as application/json. Parsing the response to an object is not required and not desired as the only thing I have to do with it is rewrite some urls and I am not sure of the content type the server will use.
To Reproduce
Server:
Client:
Outcome:
Expected behavior
response
data
should be the string"{\"doNotParse\": 1}"
, the program would print:Environment:
Additional context/Screenshots
Setting
transformResponse: (data) => data
appears to do the trick, regardless ofresponseType
.The text was updated successfully, but these errors were encountered: