Error in content type Guzzle

Asked

Viewed 124 times

0

I am using this trait as the basis of the request and am setting the Content-Type as application/vnd.api+json

trait BaseRequest
{
    private static $url = 'url';

    public static function request()
    {
        return new Client([

           'base_uri'  => Self::$url, 

            'headers'   => [

                'Content-Type'  => 'application/vnd.api+json',

                'Accept'        => 'application/vnd.api+json',

               'Api-Key'       => Config::getApiToken(),
            ] 
        ]);
   }
}

However when making the request an error is returned and when I check the Content-Type he’s like application/json

  $request = Self::request();

    try {

        $response = $request->post('orders', [

            \GuzzleHttp\RequestOptions::JSON =>  
            [
                'data' => 
                [
                    'type'        => 'orders',
                    'attributes'  => 
                    [
                        'allow_back_ordering'  => false,
                        'items'   => 
                        [
                            'type'        => 'did_order_items',
                            'attributes'  => 
                            [
                                'qty'     => 1,
                                'sku_id'  => $skuId,
                            ],
                        ],
                    ],
                ]
            ]
        ]);

        return json_decode($response->getBody()->getContents());

    } catch (\Exception $e) {
        echo($e->getRequest()->getHeader('Content-Type'));
        return false;
    }

Error returned

{  
  "errors":[  
     {  
     "title":"Unsupported media type",
     "detail":"All requests that create or update must use the 
    'application/vnd.api+json' Content-Type. This request specified 
     'application/json'.",
     "code":"415",
     "status":"415"
     }
  ]
}
  • What is the Content-Type required ? because in the answer is already different from what you put in the request!!!

  • The required Content-Type is application/vnd.api+json

  • Okay, make the error available!?

  • edited the question

  • Ali would not be $response instead of $request->getBody()->getContents()???

  • Yeah, I fixed it, same mistake

  • have to check with API about this problem? has any link?

  • I performed the requisition via Postman and it worked

Show 3 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.