verbb / consume

A Craft CMS plugin to create HTTP requests in your Twig templates to consume JSON, XML or CSV content.
Other
4 stars 1 forks source link

Fedex API - UNSUPPORTED.MEDIA.TYPE.ERROR #8

Closed bleepsandblops closed 8 months ago

bleepsandblops commented 9 months ago

Describe the bug

Using the newly added Fedex client, I am getting this back from the API: UNSUPPORTED.MEDIA.TYPE.ERROR.

See details below:

Steps to reproduce

Create the fedex client and connect - all fine.

In a plugin, do: $data = Consume::$plugin->getService()->fetchData('fedex', 'POST', '/track/v1/trackingnumbers', $payload); with $payload being:

$payload = [
            'includeDetailedScans' => true,
            'trackingInfo' => [
                [
                    'trackingNumberInfo' => [
                        'trackingNumber' => 'XXXXXXXXXX',

                    ]
                ]
            ]
        ];

with XXX being a valid tracking number.

Error in the logs:

2024-01-05 14:39:28 [ERROR] Unable to fetch data: “Client error: `POST https://apis.fedex.com/track/v1/trackingnumbers` resulted in a `415 Unsupported Media Type` response:
{"transactionId": "086ca6f7-ace3-4ed0-a552-bb476d1d78b0","errors":[{"code":"UNSUPPORTED.MEDIA.TYPE.ERROR","message":"We  (truncated...)

Now if you use Guzzle instead and do this with the same payload:

$client = Consume::$plugin->getClients()->getClientByHandle('fedex');
        $token = $client->getToken()->accessToken;
        $client = new Client();

        $headers = [
            'Authorization' => 'Bearer '.$token, // Replace with your token
            'X-locale' => 'en_US',
            'Content-Type' => 'application/json'
        ];

        $jsonPayload = json_encode($payload);
        echo $jsonPayload;

        try {
            $response = $client->request('POST', 'https://apis.fedex.com/track/v1/trackingnumbers', [
                'headers' => $headers,
                'body' => $jsonPayload
            ]);

            echo $response->getBody();
        } catch (GuzzleException $e) {
            echo $e->getMessage();
        }

You get the correct response, no error.

Is it the Content-Type' => 'application/json that's missing in the Consume call?

Craft CMS version

4.4.16.1

Plugin version

1.0.8

Multi-site?

No

Additional context

No response

engram-design commented 9 months ago

So I would suggest changing your payload to include the correct Guzzle payload, including json. We just don't include this to add flexibility with the payload to include form_params as an alternative for example.

$payload = [
    'json' => [
        'includeDetailedScans' => true,
        'trackingInfo' => [
            [
                'trackingNumberInfo' => [
                    'trackingNumber' => 'xxxxxxxxxxxxxxxxxxxx',
                ]
            ]
        ]
    ]
];

But we could probably pass in the correct header as well - I'll look into that. But this would still be considered the correct payload format according to Guzzle.

bleepsandblops commented 8 months ago

@engram-design that worked thanks! You also solved another issue I had with sending multipart so that should do the trick as well. Thanks!

Edit: the multipart is for the Trade Documents Upload API, have created an issue #9