skip to Main Content

So I’m having an interesting issue with Laravel HTTP Client while trying to hit an API endpoint for PayPal.

I can get Laravel HTTP Client working on all my endpoints that POST with data, but this one endpoint that only requires headers (no data is passed in the body) fails with an error.

{
   "name":"INVALID_REQUEST",
   "message":"Request is not well-formed, syntactically incorrect, or violates schema.",
   "debug_id":"609388c4ddfe4",
   "details":[
      {
         "field":"/",
         "location":"body",
         "issue":"INVALID_SYNTAX",
         "description":"MALFORMED_REQUEST_JSON"
      }
   ],
   "links":[
      {
         "href":"https://developer.paypal.com/docs/api/orders/v2/#error-INVALID_SYNTAX",
         "rel":"information_link",
         "encType":"application/json"
      }
   ]
}

When I hit the same endpoint in Postman everything works fine

My method for hitting the endpoint looks like this

public static function capture($order)
{
  $token =  Paypal::fetchToken();
  $api_url = config('services.paypal.api_url') . '/v2/checkout/orders/' . $order['id'] . '/capture';
  
  $headers = [
     'Content/Type'  => 'application/json',
  ];

  $response = Http::withToken($token)
  ->withHeaders($headers)
  ->post($api_url)
  ->json();

  return $response;
}
  • I have tried passing an empty array in the post request like this ->post($api_url, []) but that did not work either.
  • I have hardcoded the $api_url just in case I made a mistake with my formatting with variables. Resulted in the same issue.
  • I have tried changing the 'Content/Type' in the header to 'none'. This did not work either and also doesn’t make sense because I have this same header set in postman and it works fine (PayPal docs also says to pass this content/type)

Based on the error I am receiving I can only assume the request is hitting the endpoint correctly, but either the HTTP wrapper or guzzle itself is adding something to the body when I leave it blank and it is causing PayPal to throw the error. Don’t really know what else I can try though.

Is there a parameter I am overlooking for specifying an empty body on a post request?

Any help is appreciated.

2

Answers


  1. I had the same issue, but I fixed it with a simple trick.
    I found the solution on https://docs.guzzlephp.org/en/stable/request-options.html#json.

    This code should work.

    $response = Http::withToken($token)
      ->withHeaders($headers)
      ->post($api_url,['json' => []])
      ->json();
    

    The empty array is now seen as an empty array/body in JSON.

    Login or Signup to reply.
  2. Looking at the source I found the following solution

    $response = Http::withToken($token)
      ->withHeaders($headers)
      ->send("POST", $api_url)
      ->json();
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search