{ fir-6ren">
gpt4 book ai didi

php - Ruby httparty 发布请求

转载 作者:行者123 更新时间:2023-12-04 14:41:40 24 4
gpt4 key购买 nike

我有工作 php 代码

   <?php
$ch = curl_init("https://myurl/api/add_lead");

$first_name = $_POST["name"];
$phone = $_POST["phone"];
$email = $_POST["email"];
$ipaddress = $_SERVER['REMOTE_ADDR'];

curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_POST,true);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,1120);

curl_setopt($ch,CURLOPT_POSTFIELDS,"first_name=$first_name&phone=$phone&email=$email&ipaddress=$ipaddress");
curl_setopt($ch,CURLOPT_HTTPHEADER,["Content-Type:application/x-www-form-urlencoded; charset=utf-8"]);

curl_setopt($ch,CURLOPT_TIMEOUT,60);
$result = curl_exec($ch);

curl_close($ch);
?>

我需要在 ruby​​ 代码中转换它

我试过了
HTTParty.post("https://myurl/api/add_lead",
{
:body => { first_name: "test", phone: "123456789", email: "email@email.com" , ipaddress:'192.168.0.0'},
:headers => { 'Content-Type' => 'application/x-www-form-urlencoded','charset'=>'utf-8'}

})

但有 500 错误代码

如何正确操作?

最佳答案

请注意,在您的 PHP 代码中,您将字符串作为 POST 正文传递。
在 Ruby 代码中,您传递的是 json。

请尝试以下操作:

HTTParty.post("https://myurl/api/add_lead", {
body: "first_name=test&phone=123456789&email=email@email.com&ipaddress=192.168.0.0",
headers: {
'Content-Type' => 'application/x-www-form-urlencoded',
'charset' => 'utf-8'
}
})

关于php - Ruby httparty 发布请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45463812/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com