0

I'm building a react project and I'm using an API, in the API doc they just provided this curl command:

#!/bin/bash 
apiKey="yourApiKey"
secret="yourSecret"
curl -i \
-X GET \
-H 'Accept:application/json' \
-H 'Api-key:'$apiKey'' \
-H 'X-Signature:'$(echo -n ${apiKey}${secret}$(date +%s)|sha256sum|awk '{ print $1}')'' \
https://example.com

How can I convert this code to a JavaScript fetch?

I tried this code:

  const sec = Date.now() / 1000 + "";
  const myString = "someApiKey" + "apiSecret" + sec;
  const bitArr = sjcl.hash.sha256.hash(myString);
  const hash = sjcl.codec.hex.fromBits(bitArr);

  useEffect(() => {
    fetch("https://example.com", {
      method: "GET",
      headers: {
        Accept: "application/json",
        "content/type": "application/json",
        "Api-key":'someApiKey',
        'X-Signature': hash
      },
    })
      .then((res) => res.json())
      .then((data) => console.log(data))
      .catch((err) => console.log(err));
  }, []);

but it gave me error:

TypeError: Failed to execute 'fetch' on 'Window': Invalid name

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
droppre
  • 1
  • 1

0 Answers0