6

I want to send request with Http facade in Laravel but i can't pass json object data it must be just array.

Http::withHeaders(['Content-Type' => 'application/json'])->withOptions(['headers' => $coockie])->post($this->policy_url . $address, json_encode($data));

My data variable value:

$data = [
    "document" => [
        "customerId" => 1,
        "currencyId" => 1,
        "settlementPolicyId" => 8,
        "storeId" => 16,
        "documentPatternId" => 2,
        "items" => [
            [
              "productId" => 193,
              "unitId" => 2,
              "quantity" => 1,
              "storeId" => 16,
              "TrackingFactor1" => "1214",
              "PartTrackingFactorRef1" => 1053,
              "TrackingFactorHasQuantity1" => true,
              "TrackingFactorValue" => "‏test",
              "fee" => 0
            ]
          ]
        ],
        "payments" => [
            [
                "key" => "Cash",
                "amount" => 445810,
                "attr" => []
            ]
        ]
    ];

The error:

TypeError Argument 2 passed to Illuminate\Http\Client\PendingRequest::post() must be of the type array, string given
7
  • can you show full code what $data contain and what error are you getting Commented Aug 1, 2021 at 13:57
  • @JohnLobo Yes i added Commented Aug 1, 2021 at 14:02
  • what error are you getting ? better first check in postman whether api works with this data or not . Commented Aug 1, 2021 at 14:02
  • @JohnLobo error: TypeError Argument 2 passed to Illuminate\Http\Client\PendingRequest::post() must be of the type array, string given Commented Aug 1, 2021 at 14:05
  • $data must be array. you are passing string but as per your question $data is array.may be you are modifiying $data before passing http Commented Aug 1, 2021 at 14:07

2 Answers 2

11

You may try using withBody method

Http::withBody(json_encode($data), 'application/json')
    ->withOptions([
       'headers' => $coockie
    ])
    ->post($this->policy_url . $address);
Sign up to request clarification or add additional context in comments.

2 Comments

awesome, saved me... cant even see 'withOptions' in the docs without really digging
that's weird, since the docs says: By default, data will be sent using the application/json content type.
0

Make json_encode($data) before sending it to the request.

$myArray = json_encode($data);
 $finalData = explode(' ',$myArray);

3 Comments

TypeError Argument 2 passed to Illuminate\Http\Client\PendingRequest::post() must be of the type array, string given,
ErrorException explode(): Empty delimiter
if explode doesn't work for you use $ary = preg_split('//', $string, -1, PREG_SPLIT_NO_EMPTY); to ready properly your string. Hopefiully it can help you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.