-1

How can I convert the following CURL command to javascript fetch?

curl --request GET -L -c --url https://tableau.example.com/trusted/DjabukX7T3u_bTUT0z7NdQ==:rQS3WF-ol-5snlDTOYlQ7pS2/t/site/views/workbook_name/view_name

I used the above command on my server is working.

Then I throw this command to many online CURL to javascript fetch converters, and they all return only like this:

fetch('https://tableau.example.com/trusted/DjabukX7T3u_bTUT0z7NdQ==:rQS3WF-ol-5snlDTOYlQ7pS2/t/site/views/workbook_name/view_name');

The above fetch code is not working for me. and it returns Uncaught (in promise) TypeError: Failed to fetch.

I think the -c -L flag in CURL has the meaning for fetching:

https://curl.se/docs/manpage.html#-c

https://curl.se/docs/manpage.html#-L

What is the precise corresponding javascript fetch code for this CURL command?

thanks for the help from all of you.

Aaron
  • 61
  • 9

1 Answers1

2

Most of the flags you are using for cURL are default behaviour for fetch.

Since you are making a cross-origin request you need to set the credentials flag to include for cookies to be enabled.

Note that this will require the server to support a CORS preflight.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • fetch( "https:/tableau.example.com/trusted/" + credential + "/t/site/views/workbook_name/view_name", { credentials: "include", method: "GET", } – Aaron Oct 20 '22 at 10:15
  • is it like this? – Aaron Oct 20 '22 at 10:16
  • but it seems still not working and shows Uncaught (in promise) TypeError: Failed to fetch – Aaron Oct 20 '22 at 10:16
  • Are there any other error messages? See my note in the answer about CORS preflights. – Quentin Oct 20 '22 at 10:17
  • hmmm no other messages , just the error I mentioned above – Aaron Oct 20 '22 at 10:18
  • The console usually does show a more specific error when "TypeError: Failed to fetch" is the only message exposed to the JS code itself. – Quentin Oct 20 '22 at 10:19
  • oh ok it shows this : Ensure CORS response header values are valid – Aaron Oct 20 '22 at 10:24
  • Looks like you need to change the server side code on tableau.example.com to grant you permission then. https://stackoverflow.com/a/35553666/19068 – Quentin Oct 20 '22 at 10:25
  • so it means server need to support CORS right? even though I can get content by CURL – Aaron Oct 20 '22 at 10:26
  • 1
    Yes. Your cURL does not have access to the cookies already in the browsers of visitors to your website. Doing stuff across origins in browsers requires permission. – Quentin Oct 20 '22 at 10:27
  • but CURL doesnt means javascript can get also, javascript need CORS to support. am I right? – Aaron Oct 20 '22 at 10:27