0

The website CERMINE has provided REST service with cURL tools. They give the following example:

curl -X POST --data-binary @article.pdf \
  --header "Content-Type: application/binary"\
  http://cermine.ceon.pl/extract.do

How can I convert the following into JavaScript?

Edit #1: So the fetch would look like this?

fetch("http://cermine.ceon.pl/extract.do", { 
  body: "@article.pdf", 
  headers: { 
    "Content-Type": "application/binary\\"
  },
  method: "POST"
})
Phil
  • 157,677
  • 23
  • 242
  • 245
Saman Ray
  • 115
  • 2
  • 11
  • https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API (newer, promised based) or https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest (older, callback based) – Phil Mar 27 '19 at 05:22
  • Keep in mind, if you run this in a browser, you are subject to the same origin policy. See https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS – Phil Mar 27 '19 at 05:22
  • Looks like that site supports CORS response headers so you're in luck – Phil Mar 27 '19 at 05:24
  • How are you planning on getting the `article.pdf` file to your script? Is this to run in a browser or via Node? – Phil Mar 27 '19 at 05:28
  • @Phil The article.pdf will be on the browser. – Saman Ray Mar 27 '19 at 05:29
  • Possible duplicate of [how to get files from (Indirect) with javascript](https://stackoverflow.com/questions/15791279/how-to-get-files-from-input-type-file-indirect-with-javascript) – Phil Mar 27 '19 at 05:32
  • @Phil It gives me this error: **Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://cermine.ceon.pl/extract.do. (Reason: missing token ‘content-type’ in CORS header ‘Access-Control-Allow-Headers’ from CORS preflight channel).** – Saman Ray Mar 27 '19 at 05:35
  • Oh, guess they don't allow you to do this via JS then. – Phil Mar 27 '19 at 05:37
  • you need to use FormData() to upload files with fetch(), as a "spoofed" file, or, you can feed it your actual whole form, a trick a lot of coders forget about... – dandavis Mar 27 '19 at 05:38
  • @dandavis So how would the code look? Sorry, I have no idea about FormData() – Saman Ray Mar 27 '19 at 05:44
  • google "upload file with js formdata" for a walk-through – dandavis Mar 27 '19 at 05:45

0 Answers0