title: API returning 400 Bad Request response
body: <p>I have built an API and an application that uses that API.  Everything was working but now, for some reason, I get a 400 Bad Request response.  I am not sure if I changed something in the code so I wanted to double check it was correct.</p>
  
  <p>So my API call is this</p>
  
  <pre><code>$client = new GuzzleHttp\Client();
  
  $jsonData = json_encode($data);
  
  $req = $client-&gt;request('POST', 'https://someurl.com/api/v1/createProject', [
      'body' =&gt; $jsonData,
      'headers' =&gt; [
          'Content-Type' =&gt; 'application/json',
          'Content-Length' =&gt; strlen($jsonData),
      ]
  ]);
  
  $output = $req-&gt;getBody()-&gt;getContents();
  </code></pre>
  
  <p>The API has a route set up correctly which uses post.  The function it calls is correct, and I have changed it for testing to simply return </p>
  
  <pre><code>return response()-&gt;json(["Success", 200]);
  </code></pre>
  
  <p>When I test the API out within Postman, I can see that Success is returned.  When I test the API within the other application I have built, I dont even see a POST request within the console, I am just displayed a Laravel error 400 Bad Request.</p>
  
  <p>What could be the cause of this issue?</p>
  
  <p>Thanks</p>
  
  <p><strong>Update</strong></p>
  
  <p>I have changed the request to this</p>
  
  <pre><code>$data= json_encode($data);
  
  $req = $client-&gt;post('https://someurl.com/api/v1/createProject', [
      'body' =&gt; $data
  ]);
  </code></pre>
  
  <p>If I output <code>$data</code> after it has been encoded, I get something like this</p>
  
  <pre><code>{
      "projectName":"New Project",
      "clientName":"Test Client",
  }
  </code></pre>
  
  <p>Within the controller function of the API that is being called, I simply do</p>
  
  <pre><code>return response()-&gt;json(['name' =&gt; $request-&gt;input('clientName')]);
  </code></pre>
  
  <p>The 400 error has now gone, but I now get null returned to me</p>
  
  <pre><code>{#326 ▼
    +"name": null
  }
  </code></pre>
  
  <p>Request is being injected into the function as it should be.  Should I be returning the data in a different way?</p>
  
  <p>Thanks</p>