Curl-convert 到一个简单的 http 请求?

Curl-convert to a simple http request?

我有一些 curl 请求,我想从中构建一个基本的 POST request-authenticated(使用 headers 等),我不能找到任何可以转换它的工具:

curl -X POST 'https://api.twilio.com/2010-04-01/Accounts/xxxxxxxxxxxxx/Calls.json' \
--data-urlencode 'To=xxxxxxx65542'  \
--data-urlencode 'From=+xxxxxxx4215'  \
-d 'Url=https://api.twilio.com/2010-04-01'  \
-d 'Method=GET'  \
-d 'FallbackMethod=GET'  \
-d 'StatusCallbackMethod=GET'  \
-d 'Record=false' \
-u ACbe68cddxxxxxxxxxxxx3aba243cc4cdb:0f442xxxxxxxxxxxxxxxxxxx

那么我的 POST 请求应该是什么样子的?

来自 cURL 手册页:

-H, --header (HTTP) Extra header to use when getting a web page. You may specify any number of extra headers. Note that if you should add a custom header that has the same name as one of the internal ones curl would use, your externally set header will be used instead of the internal one. This allows you to make even trickier stuff than curl would nor‐ mally do. You should not replace internally set headers without knowing perfectly well what you're doing. Remove an internal header by giving a replacement without content on the right side of the colon, as in: -H "Host:". If you send the custom header with no-value then its header must be terminated with a semicolon, such as -H "X-Custom- Header;" to send "X-Custom-Header:".

curl will make sure that each header you add/replace is sent with the proper end-of-line marker, you should thus not add that as a part of the header content: do not add newlines or carriage returns, they will only mess things up for you.

See also the -A, --user-agent and -e, --referer options.

This option can be used multiple times to add/replace/remove multiple headers.

Amazon AWS 大量使用 headers 进行身份验证。快速 Google 应该会引出许多示例,例如来自 http://tmont.com/blargh/2014/1/uploading-to-s3-in-bash 的示例:

  curl -X PUT -T "${file}" \
  -H "Host: ${bucket}.s3.amazonaws.com" \
  -H "Date: ${dateValue}" \
  -H "Content-Type: ${contentType}" \
  -H "Authorization: AWS ${s3Key}:${signature}" \
  https://${bucket}.s3.amazonaws.com/${file}

您需要做的就是调整 AWS 示例以供您使用 twilio。

要将示例变成 POST,只需将 PUT 更改为 POST 并添加您的 POST 字段 -d "field1=val1&field2=val2&field3=val3"

您可以使用 --header 指定 headers 并使用 -X / --request 参数指定请求类型 (POST)。

示例:

curl --request POST --header "X-MyHeader: MyAuthenticatedHeader" www.whosebug.com

你的情况应该是:

curl -X POST 'https://api.twilio.com/2010-04-01/Accounts/xxxxxxxxxxxxx/Calls.json' \
--header 'X-MyHeader: MyAuthenticatedHeader' \
--data-urlencode 'To=xxxxxxx65542'  \
--data-urlencode 'From=+xxxxxxx4215'  \
-d 'Url=https://api.twilio.com/2010-04-01'  \
-d 'Method=GET'  \
-d 'FallbackMethod=GET'  \
-d 'StatusCallbackMethod=GET'  \
-d 'Record=false' \
-u ACbe68cddxxxxxxxxxxxx3aba243cc4cdb:0f442xxxxxxxxxxxxxxxxxxx

好的,所以我想弄清楚如何从 ESP8266 nodeMcu v0.9 模块发送短信。它能够像 arduino 一样与 arduino ide 1.6.4.

一起工作

无论如何,我找到了 http://textbelt.com,它只显示了发送短信的简单 CURL 方式。

这是它要您发送的 CURL 消息

$ curl -X POST http://textbelt.com/text \ -d number=5551234567 \ -d "message=I sent this message for free with textbelt.com"

因此,为了转换为普通的 HTTP POST 命令,我执行了以下操作。 (这适用于 Arduino IDE) number 和 message 是 String 对象。

String messageToSend = "number="+number+"&message="+message;

client.print("POST /text HTTP/1.1\r\n");
client.print("Host: textbelt.com\r\n");
client.print("Content-Type: application/x-www-form-urlencoded\r\n");
client.print("Content-Length: ");
client.print(messageToSend.length());
client.print("\r\n\r\n");
client.print(messageToSend);

起初我尝试不使用 Content-Type,但这似乎不起作用。所以我也必须添加我发送的内容类型。

如果您使用 WireShark 监控网络流量,您会看到

POST /text HTTP/1.1\r\n
Host: textbelt.com\r\n
Content-Type: application/x-www-form-urlencoded\r\n
Content-Length: 48\r\n
\r\n
number=5551234567&message=this is a text message

我可能已经能够使用较少的文本 text/plain 作为内容类型,但我认为它可能需要应用程序 urlencoded 类型才能工作。

希望这对尝试将 curl 转换为 http 的其他人有所帮助。