0

I'm trying to send an HTTP request from my react code to iphub API.

in the document there is an example of how to use in this service:

curl http://v2.api.iphub.info/ip/8.8.8.8 -H "X-Key: 123"

I convert the request according to some answers and it looks like this:

const ripeEndpoint = 'http://v2.api.iphub.info/ip/8.8.8.8'
fetch(ripeEndpoint, {
  method: 'GET',
  headers: new Headers({
    'Access-Control-Allow-Origin': '*',
    'Content-Type': 'multipart/form-data',
    'X-Key': myApiKey
  }),
})

but the response is 404, I think that my request is wrong, how can I know if the converted request correct?

B--rian
  • 5,578
  • 10
  • 38
  • 89
j.her
  • 187
  • 1
  • 2
  • 11
  • 1
    `Access-Control-Allow-Origin` is a _response_ header, trying to specify it as a _request_ header makes zero sense. – 04FS Sep 26 '19 at 09:32
  • `'Content-Type': 'multipart/form-data'` – why? There is nothing in your cURL command that would cause this to be used. – 04FS Sep 26 '19 at 09:34
  • Is this API even CORS-enabled …? – 04FS Sep 26 '19 at 09:40

2 Answers2

0

You are mixing up response and request headers.

A response header is an HTTP header that can be used in an HTTP response and that doesn't relate to the content of the message. Response headers, like Age, Location or Server are used to give a more detailed context of the response. ... However, these entity requests are usually called responses headers in such a context.

Source: https://developer.mozilla.org/en-US/docs/Glossary/Response_header

A request header is an HTTP header that can be used in an HTTP request, and that doesn't relate to the content of the message. Request headers, like Accept, Accept-*, or If-* allow to perform conditional requests; others like Cookie, User-Agent or Referer precise the context so that the server can tailor the answer.

Source: https://developer.mozilla.org/en-US/docs/Glossary/Request_header

For a simple GET request, you usually do not need any header at all, see HTTP GET request in JavaScript? In your case, in you curl command, you specified a single additional header, and so you might just do that in Javascript as well.

B--rian
  • 5,578
  • 10
  • 38
  • 89
0

Try this out:

<?php
$ipaddress = $_SERVER['REMOTE_ADDR'];
$url = 'http://v2.api.iphub.info/ip/'.$ipaddress;
$auth = "OTf4sfpNHUhRM1AwNVMOZlJPSmRseTY0WTFsMGhtR1J0alhEbW=="; 
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, ["X-Key: {$auth}"]);
$result = curl_exec($ch);
echo $result;
curl_close($ch);
?>
Dharman
  • 30,962
  • 25
  • 85
  • 135