简体   繁体   English

如何将 curl 请求转换为 javascript

[英]How to convert curl request to javascript

I'm trying to send an HTTP request from my react code to iphub API.我正在尝试从我的反应代码向 iphub API 发送 HTTP 请求。

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?但是响应是404,我认为我的请求是错误的,我怎么知道转换后的请求是否正确?

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. 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.响应标头,例如AgeLocationServer用于提供更详细的响应上下文。 ... However, these entity requests are usually called responses headers in such a context. ...但是,在这种情况下,这些实体请求通常称为响应标头。

Source: https://developer.mozilla.org/en-US/docs/Glossary/Response_header资料来源: 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. 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;请求标头,如AcceptAccept-*If-*允许执行条件请求; others like Cookie , User-Agent or Referer precise the context so that the server can tailor the answer.其他类似CookieUser-AgentReferer的工具可以精确上下文,以便服务器可以定制答案。

Source: https://developer.mozilla.org/en-US/docs/Glossary/Request_header来源: 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?对于一个简单的GET请求,您通常根本不需要任何 header,请参阅JavaScript 中的 HTTP GET 请求? In your case, in you curl command, you specified a single additional header, and so you might just do that in Javascript as well.在您的情况下,在您的curl命令中,您指定了一个额外的 header,因此您也可以在 Javascript 中执行此操作。

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);
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM