简体   繁体   English

如何以json格式设置请求正文

[英]How to set the request body in a json format

I'm trying to send a request body in a json format to an API. 我正在尝试将json格式的请求正文发送到API。 The docs state the body needs to be formatted like so: 文档指出正文需要采用以下格式:

//Request body needs to the contain a JSON object in the following format:

{  
  "delete_list":[  
    "8fcd1d68c82dd39d65ef8ea9a7948bbe",
    "8bca1d68c92dd39d65ef8ea9a7948bbe",
    "8bca1d69c82d939d65ef8ea9a7948bbe",
    "8bca1d69c82d939d65ef8ea9a7947bbe"
   ]
}

My attempted code seems to fail and I am not sure why. 我尝试的代码似乎失败了,我不确定为什么。 I've tried quite a few adjustments and nothing seems to work: 我尝试了很多调整,但似乎没有任何效果:

    $del = array("delete_list" => "[8bca1d69c82d939d65ef8ea9a7947bbe]");
    $j = json_encode($del);


    $apiToken = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';

    $httpHeadersArray = Array();
    $httpHeadersArray[] = 'Authorization: key='.$apiToken;
    $httpHeadersArray[] = 'Content-Type: application/json';


    //open connection
    $ch = curl_init();

    //set the url, number of POST vars, POST data
    curl_setopt($ch, CURLOPT_URL, $curlUrl);
    #curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($j));
    curl_setopt($ch, CURLOPT_HTTPHEADER, $httpHeadersArray);


    $res = curl_exec($ch);


    echo "<pre>";
    print_r($res);
    echo "</pre>";

My end result that I get back from the API is as follows: 我从API中获得的最终结果如下:

{"status":"failure","message":"JSON malformed"}

Any suggestions as to what I am doing wrong with the code? 关于我在代码中做错什么的任何建议? It would be greatly appreciated. 这将不胜感激。 Thanks! 谢谢!

Two things: 两件事情:

1) You're json encoding an object with a single string but you need to encode an object with an array. 1)您使用单个字符串对JSON进行编码,但需要使用数组对对象进行编码。

2) You're using http_build_query which will urlencode the array arguments and break the JSON structure. 2)您正在使用http_build_query ,它将对数组参数进行urlencode并破坏JSON结构。

Do this: 做这个:

    $del = array("delete_list" => [ "8bca1d69c82d939d65ef8ea9a7947bbe" ]);
    $j = json_encode($del);


    $apiToken = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';

    $httpHeadersArray = Array();
    $httpHeadersArray[] = 'Authorization: key='.$apiToken;
    $httpHeadersArray[] = 'Content-Type: application/json';


    //open connection
    $ch = curl_init();

    //set the url, number of POST vars, POST data
    curl_setopt($ch, CURLOPT_URL, $curlUrl);
    #curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
    curl_setopt($ch, CURLOPT_POSTFIELDS, $j);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $httpHeadersArray);


    $res = curl_exec($ch);


    echo "<pre>";
    print_r($res);
    echo "</pre>";

将标题扔到顶部

header('Content-Type: application/json');

Yes you are malforming the json or indeed the array 是的,您是在错误地构造json或数组

$del = array("delete_list" => array("8bca1d69c82d939d65ef8ea9a7947bbe")); $j = json_encode($del);

This is the correct way, you are passing the array as a string, which won't work. 这是正确的方法,您将数组作为字符串传递,这是行不通的。

json_encode() returns a string containing the JSON representation of value. json_encode()返回一个字符串,其中包含value的JSON表示形式。

PHP Manual PHP手册

It returns a representation of a value. 它返回值的表示形式。

print_r(array(
    "delete_list" => "[8bca1d69c82d939d65ef8ea9a7947bbe]"
));

Prints: 打印:

{
    "delete_list": "[8bca1d69c82d939d65ef8ea9a7947bbe]"
}

Whereas: 鉴于:

print_r(array(
    "delete_list" => array(
        "8bca1d69c82d939d65ef8ea9a7947bbe"
    )
));

Prints: 打印:

{
    "delete_list": [
        "8bca1d69c82d939d65ef8ea9a7947bbe"
    ]
}

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

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