通过curl发送POST到服务器不起作用

时间:2018-03-22 04:56:30

标签: php curl http-post

我正在尝试向localhost:9090上运行的服务器(JBOSS)发送POST请求,如下所示,但它似乎无法正常工作。我的意思是,我没有看到我的网络服务被执行。我在下面做了什么吗?

由于$_POST["mydata"]包含以下内容:

[{ "name": "FirstName", "value": "Mickey" }, { "name": "LastName", "value": "Mouse" }]

这是在javascript对象上运行JSON.stringify()的结果(使用serializeArray()),我没有在标题中发送application/json相关内容。

$urlTest = 'http://localhost:9090/JAXRS_POST_Request/rest/Request/insertDataToDB';

        $rCURL = curl_init();
        curl_setopt($rCURL, CURLOPT_CUSTOMREQUEST, "POST"); 
        curl_setopt($rCURL, CURLOPT_POSTFIELDS, $_POST["myData"]);
        curl_setopt($rCURL, CURLOPT_URL, $urlTest);
        curl_setopt($rCURL, CURLOPT_HEADER, 0);
        curl_setopt($rCURL, CURLOPT_RETURNTRANSFER, 1);
        $response_post = curl_exec($rCURL);
        curl_close($rCURL);

但是如果我必须使用像POSTMAN这样的客户端手动测试web服务,我必须在标题下指定为Content-Type : application/json并通过Body发送它,并且它在那里工作得很好:

enter image description here

但是当通过curl发送请求时,由于JSON.stringify()将javascript对象转换为字符串以将其发送到服务器,我很困惑我是否需要在POST请求中使用header thing。

P.S。一个简单的基于curl的GET请求对我来说很好,所以我的应用程序成功地联系了服务器。

1 个答案:

答案 0 :(得分:0)

试试这个:

<tr id="1">
  <td style="color : blue;"><b>President Staff</b></td>
  <td class="text-center" style="padding-left:40px;"><input type="radio" class="form-check-input" name="jabatan1"  value="green" onClick="green('1')" required></td>
  <td class="text-center" style="padding-left:40px;"><input type="radio" class="form-check-input" name="jabatan1"  value="red" onClick="red('1')" required></td>
</tr>

<tr id="2">
  <td>Corporate Planning & Program Office</td>
  <td class="text-center" style="padding-left:40px;"><input type="radio" class="form-check-input" name="jabatan2"  value="green" onClick="green('2')" required></td>
  <td class="text-center" style="padding-left:40px;"><input type="radio" class="form-check-input" name="jabatan2"  value="red" onClick="red('2')"required></td>
</tr>

<tr id="3">
  <td>Acquisitiom & Aircraft Management</td>
  <td class="text-center" style="padding-left:40px;"><input type="radio" class="form-check-input" name="jabatan3"  value="green" onClick="green('3')"required></td>
  <td class="text-center" style="padding-left:40px;"><input type="radio" class="form-check-input" name="jabatan3"  value="red" onClick="red('3')"required></td>
</tr>

<tr id="4">
  <td>Corporate Quality, Safety & Environmentak Management</td>
  <td class="text-center" style="padding-left:40px;"><input type="radio" class="form-check-input" name="jabatan4"  value="green" onClick="green('4')"required></td>
  <td class="text-center" style="padding-left:40px;"><input type="radio" class="form-check-input" name="jabatan4"  value="red" onClick="red('4')" required></td>
</tr>

<tr id="5">
  <td>Corporate Secretary</td>
  <td class="text-center" style="padding-left:40px;"><input type="radio" class="form-check-input" name="jabatan5"  value="green" onClick="green('5')" required></td>
  <td class="text-center" style="padding-left:40px;"><input type="radio" class="form-check-input" name="jabatan5"  value="red" onClick="red('5')" required></td>
</tr>

<tr id="6">
  <td>Internal Audit</td>
  <td class="text-center" style="padding-left:40px;"><input type="radio" class="form-check-input" name="jabatan6"  value="green" onClick="green('6')" required></td>
  <td class="text-center" style="padding-left:40px;"><input type="radio" class="form-check-input" name="jabatan6"  value="red" onClick="red('7')" required></td>
</tr>