Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
bleepsandblops opened this issue Jan 5, 2024 · 2 comments
Closed

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

bleepsandblops opened this issue Jan 5, 2024 · 2 comments

Comments

@bleepsandblops
Copy link

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
Copy link
Member

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
Copy link
Author

bleepsandblops commented Jan 11, 2024

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants