HTTP请求失败!HTTP / 1.1 400错误请求

时间:2023-01-13 18:32:43

I am trying to create a issue in Jira .

我想在Jira里制造一个问题。

I am able to make a GET request with a proper response, but the problem arises when i make a POST request.Here is the code.

我可以用适当的响应发出GET请求,但是当我发出POST请求时,问题就出现了。这是代码。

<?php

$userName ='xxxxxxxxxxxxxxxx';
$password ='xxxxxxxxxxxx';

$data = ['fields' => ['projects'=>['key'=>'ABC'],'summary'=>'abc','description'=>'abc','issuetype'=>['name'=>'Task']]];

$data = http_build_query($data);
$context = 
     array("http"=>
        array(
            "method" => "POST",
                "header" => "Authorization: Basic " . base64_encode($userName.':'.$password) . "\r\n".
                'Accept: application/json'."\r\n".
                "Content-Length: ".strlen($data)."\r\n".
                    'Content-Type: application/json'."\r\n",
            'content' => $data,
            'verify_peer'=>false,
            'verify_peer_name'=>false,
            )
            );
$context = stream_context_create($context);
$result = file_get_contents("https://xxxxx.atlassian.net/rest/api/2/issue/", false, $context);
echo "The result is ", $result;

?>

? >

I get the following error:

我得到以下错误:

Warning:file_get_contents(https://xxxxx.atlassian.net/rest/api/2/issue/):
failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request in
/var/www/html/test/new.php on line 27

Could any one help me out? Thanks

有人能帮我一下吗?谢谢

P.S

附:

I dont want to use curl as an alternative to http-streams as google app engine does not support curl.

我不想使用curl来替代http流,因为谷歌应用引擎不支持curl。

2 个解决方案

#1


2  

http_build_query() generates a url-encoded string. However, the API requires JSON. You should be using json_encode() instead.

http_build_query()生成url编码的字符串。但是,API需要JSON。您应该使用json_encode()。

Change:

变化:

$data = http_build_query($data);

To:

:

$data = json_encode($data);

While maybe not your only problem, this is definitely one problem that would result in an 400 Bad Request.

虽然这可能不是你唯一的问题,但这绝对是一个会导致400个错误请求的问题。

#2


0  

Could you have a typo in your code?

你的代码中有错误吗?

$data=array('fields' => array ('project' => array ('key' => 'WOIS',),'summary' => 'ABC','description' => 'ABC','issuetype' => array ('name' => 'Task',),),); 

vs

vs

$data = ['fields' => ['projects'=>['key'=>'WOIS'],'summary'=>'adfsdf','descriptio‌​n'=>'adfefsa','issue‌​type'=>['name'=>'Tas‌​k']]];

the first line says project while the second line says projects

第一行写项目,第二行写项目

#1


2  

http_build_query() generates a url-encoded string. However, the API requires JSON. You should be using json_encode() instead.

http_build_query()生成url编码的字符串。但是,API需要JSON。您应该使用json_encode()。

Change:

变化:

$data = http_build_query($data);

To:

:

$data = json_encode($data);

While maybe not your only problem, this is definitely one problem that would result in an 400 Bad Request.

虽然这可能不是你唯一的问题,但这绝对是一个会导致400个错误请求的问题。

#2


0  

Could you have a typo in your code?

你的代码中有错误吗?

$data=array('fields' => array ('project' => array ('key' => 'WOIS',),'summary' => 'ABC','description' => 'ABC','issuetype' => array ('name' => 'Task',),),); 

vs

vs

$data = ['fields' => ['projects'=>['key'=>'WOIS'],'summary'=>'adfsdf','descriptio‌​n'=>'adfefsa','issue‌​type'=>['name'=>'Tas‌​k']]];

the first line says project while the second line says projects

第一行写项目,第二行写项目