2017-08-02 199 views
0

我有工作的PHP代碼紅寶石httparty POST請求

<?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 protected]" , ipaddress:'192.168.0.0'}, 
:headers => { 'Content-Type' => 'application/x-www-form-urlencoded','charset'=>'utf-8'} 

}) 

但已經得到500錯誤代碼

如何正確地做到這一點?

回答

2

請注意,在您的PHP代碼中,您傳遞的是字符串作爲POST正文。
在Ruby代碼中,您傳遞了一個json。

嘗試以下操作:

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