0

Code below. I console.log the following error via Components/Pages/Scrap.tsx.

{status: 'PARSING_ERROR', originalStatus: 200, data: '<!DOCTYPE html>\n<html lang="en">\n  <head>\n    <met…uild` or `yarn build`.\n    -->\n  </body>\n</html>\n', error: `SyntaxError: Unexpected token '<', "<!DOCTYPE "... is not valid JSON`}
data: "<!DOCTYPE html>\n<html lang=\"en\">\n  <head>\n    <meta charset=\"utf-8\" />\n    <link rel=\"icon\" href=\"/favicon.ico\" />\n    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\" />\n    <meta name=\"theme-color\" content=\"#000000\" />\n    <meta\n      name=\"description\"\n      content=\"Web site created using create-react-app\"\n    />\n    <link rel=\"apple-touch-icon\" href=\"/logo192.png\" />\n    <!--\n      manifest.json provides metadata used when your web app is installed on a\n      user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/\n    -->\n    <link rel=\"manifest\" href=\"/manifest.json\" />\n    <!--\n      Notice the use of  in the tags above.\n      It will be replaced with the URL of the `public` folder during the build.\n      Only files inside the `public` folder can be referenced from the HTML.\n\n      Unlike \"/favicon.ico\" or \"favicon.ico\", \"/favicon.ico\" will\n      work correctly both with client-side routing and a non-root public URL.\n      Learn how to configure a non-root public URL by running `npm run build`.\n    -->\n    <title>React App</title>\n  <script defer src=\"/static/js/bundle.js\"></script></head>\n  <body>\n    <noscript>You need to enable JavaScript to run this app.</noscript>\n    <div id=\"root\"></div>\n    <!--\n      This HTML file is a template.\n      If you open it directly in the browser, you will see an empty page.\n\n      You can add webfonts, meta tags, or analytics to this file.\n      The build step will place the bundled scripts into the <body> tag.\n\n      To begin the development, run `npm start` or `yarn start`.\n      To create a production bundle, use `npm run build` or `yarn build`.\n    -->\n  </body>\n</html>\n"
error: "SyntaxError: Unexpected token '<', \"<!DOCTYPE \"... is not valid JSON"
originalStatus: 200
status: "PARSING_ERROR"
[[Prototype]]: Object

some investigation revealed in the network tab of dev tools that RTK query seems to be trying to query http://localhost:3000/scrape?url=https://somewebsite.com instead of the expected endpoint of https://localhost:7125/scrape?url=https://somewebsite.com

api/scrape-api.ts

import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react';
import type { scrapeModel } from '../models/scrape-model';

export const scrapeApi = createApi({
    reducerPath: 'scrapeApi',
    baseQuery: fetchBaseQuery({ baseUrl: 'https://localhost:7125/' }),
    endpoints: builder => ({
        scrapeUrl: builder.query<scrapeModel, string>({
            query: url => `scrape?url=${url}`
        })
    })
});

export const { useScrapeUrlQuery } = scrapeApi;

store/index.ts

import { configureStore } from '@reduxjs/toolkit';
import { setupListeners } from '@reduxjs/toolkit/dist/query';
import { scrapeApi } from '../api/scrape-api';
import scrapeReducer from './scrape';

export const store = configureStore({
    reducer: {
        [scrapeApi.reducerPath]: scrapeApi.reducer,
        scrape: scrapeReducer
    },
    middleware: getDefaultMiddleware => {
        return getDefaultMiddleware().concat(scrapeApi.middleware);
    }
});

setupListeners(store.dispatch);

export type RootState = ReturnType<typeof store.getState>;

Components/Pages/Scrap.tsx

import { useEffect } from 'react';
import { useScrapeUrlQuery } from '../../api/scrape-api';

const Scrape = () => {
    const { data, error, isLoading } = useScrapeUrlQuery(
        'https://somewebsite.com'
    );

    useEffect(() => {
        if (error) {
            console.log(error);
        }
    }, [data, error, isLoading]);

    return (
        <div>
            <h1>I'm the scrape page</h1>
            {isLoading && <div>Loading...</div>}
        </div>
    );
};

export default Scrape;
Jason Braswell
  • 301
  • 1
  • 2
  • 7

2 Answers2

0

Hope you got it resolved already, I had the same issue (GH discussion) as you, also sending an URL to a backend.

In my case the request went to localhost:5173 (the port Vite is running the react app on).

My issue was sending the raw URL. I've added an encodeURIComponent to my URL, after this the request was sent to the specified URL in my RTK api.

timhi
  • 21
  • 2
  • This does not really answer the question. If you have a different question, you can ask it by clicking [Ask Question](https://stackoverflow.com/questions/ask). To get notified when this question gets new answers, you can [follow this question](https://meta.stackexchange.com/q/345661). Once you have enough [reputation](https://stackoverflow.com/help/whats-reputation), you can also [add a bounty](https://stackoverflow.com/help/privileges/set-bounties) to draw more attention to this question. - [From Review](/review/late-answers/34458186) – user16217248 Jun 02 '23 at 01:13
  • How does this not answer the question? I had the same problem as OP and described my solution to it, it is reproduceable that the unescaped URL triggers this behaviour. – timhi Jun 02 '23 at 08:09
0

In api/scrape-api.ts change it to

scrapeUrl: builder.query<scrapeModel, string>({
query: url => `scrape?url=${encodeURIComponent(url)}`

})