1

i am using yii2 and guzzle version 4 when for api pyhton Django 1.5 when i am trying to post data with postman is working but when i am trying with php is saying bad request 404

error is GuzzleHttp\Exception\ClientException
v1/jobs/ [status code] 400 [reason phrase] BAD REQUEST

post man format data working data format

url -vm.xxx.com/v1/jobs/
 {
      "job_position":"Test Postion",
      "eligibility_course_id":["1","2","3","4"],
      "eligibiltiy_course_ids":["1","2","3","4"],
      "eligibility_branch_id":["5","6","7","8"],
      "eligibiltiy_skill_ids":["10","23","24"],
      "eligibiltiy_sublocation_ids":["10","23","24"],
      "location":["26","30","2","3"],
      "job_category":["1","4","7"],
      "sms_category":"1",
      "posting_date":"31\/07\/2014",
      "expiry_date":"31\/08\/2014",
      "job_type_id":"1",
      "job_type_other":"",
      "hiring_process":["1"],
      "job_description":"Test Description<\/p>",
      "company_name":"Test Company name",
      "company_profile":"Test company profile<\/p>",
      "company_url":"",
      "seo_title":"Test Seo Title",
      "seo_description":"Test Seo Description",
      "seo_keyword":"Test seo Keyward",
      "response":"1","how_to_apply":"",
      "terms_agreement":"1",
      "specify_terms_agreement":"",
      "employer_id":"1008369",
      "button_submitForm":"Post Job",
      "posted_by_id":"114",
      "template_name":"job_description"
    }

with php code method -1

$arr=[
"job_position" => "Test Postion",
"eligibility_course_id" => ["1","2","3","4"],
"eligibiltiy_course_ids" => ["1","2","3","4"],
"eligibility_branch_id" => ["5","6","7","8"],
"eligibiltiy_skill_ids" => ["10","23","24"],
"eligibiltiy_sublocation_ids" => ["10","23","24"],
"location" => ["26","30","2","3"],
"job_category" => ["1","4","7"],
"sms_category" => "1",
"posting_date" => "31\/07\/2014",
"expiry_date" => "31\/08\/2014",
"job_type_id" => "1",
"job_type_other" => "",
"hiring_process" => ["1"],
"job_description" => "Test Description<\/p>",
"company_name" => "Test Company name",
"company_profile" => "Test company profile<\/p>",
"company_url" => "",
"seo_title" => "Test Seo Title",
"seo_description" => "Test Seo Description",
"seo_keyword" => "Test seo Keyward",
"response" => "1","how_to_apply" => "",
"terms_agreement" => "1",
"specify_terms_agreement" => "",
"employer_id" => "1008369",
"button_submitForm" => "Post Job",
"posted_by_id" => "114",
"template_name" => "job_description"
];
$client = new Client();
$apiurl=Yii::$app->params['apiurl'];
$url=$apiurl.'/v1/jobs/';
try {

       $response = $client->post($url, ['json' => $arr]);
}
catch (\GuzzleHttp\Exception\BadResponseException $e) {

    $req = $e->getRequest();
    $resp =$e->getResponse();
    echo "<pre>";
    var_dump($req);
    var_dump($resp);
    die('ss');
}

try method-2

 $arr=json_encode($arr);
$request = $client->post($url,array(
    'content-type' => 'application/json'
),array());
$request->setBody($arr); #set body!
$response = $request->send();

then i am getting

InvalidArgumentException
No method is configured to handle the content-type config key
RahulG
  • 1,028
  • 1
  • 15
  • 27
  • Postman silently sends a lot of headers automatically, perhaps you need to include a `Content-Type` header in your `guzzle` request? – Ankh Jun 26 '15 at 09:16
  • adding Content-Type :application/json InvalidArgumentException No method is configured to handle the content-type config key – RahulG Jun 26 '15 at 09:21
  • You may also need to `json_encode` your `$arr` before sending it, [like this](http://stackoverflow.com/questions/22244738/how-can-i-use-guzzle-to-send-a-post-request-in-json) – Ankh Jun 26 '15 at 09:24
  • after json_encode same error InvalidArgumentException No method is configured to handle the content-type config key – RahulG Jun 26 '15 at 09:32
  • 1
    Try your original method `$response = $client->post($url, ['json' => $arr]);` but make sure `$arr` is `json_encode`d? – Ankh Jun 26 '15 at 09:55

1 Answers1

0

this is my mistake i am passing wrong date for-mate

"posting_date" => "31\/07\/2014",
"expiry_date" => "31\/08\/2014",

instead of this i have pass

"posting_date" => "31/07/2014",
"expiry_date" => "31/08/2014",

then is working

RahulG
  • 1,028
  • 1
  • 15
  • 27