Granga / etsy-ts

Etsy API wrapper written in typescript
https://www.npmjs.com/package/etsy-ts
MIT License
37 stars 5 forks source link

Etsy API for createReceiptShipment uses application/x-www-form-urlencoded #12

Closed lassesteffen closed 2 years ago

lassesteffen commented 2 years ago

The code currently works with a JSON encoded body. The body should actually use application/x-www-form-urlencoded. Maybe it can be changed here.

Working code for createReceiptShipment:

            var urlencoded = new URLSearchParams();
            urlencoded.append("tracking_code", data.tracking_code);
            urlencoded.append("carrier_name", data.carrier_name);
            urlencoded.append("send_bcc", data.send_bcc);
            return _this.http.request((0, tslib_1.__assign)({ path: "/v3/application/shops/" + shopId + "/receipts/" + receiptId + "/tracking", method: "POST", body: urlencoded, secure: true, type: http_client_1.ContentType.UrlEncoded, format: "json" }, params));

Solution

Granga commented 2 years ago

Hi. I've just created a shipment just by using the following code with API v3.

let shipment = await client.ShopReceipt.createReceiptShipment(
    88888888,
    99999999,
    {
        carrier_name: "ups",
        send_bcc: false,
        tracking_code: "555015043132"
    },
    tokens
);

Axios request headers contained "Content-Type": "application/x-www-form-urlencoded". The data sent was: {"carrier_name":"ups","send_bcc":false,"tracking_code":"555015043132"}.

Do you get an error when running your code?

lassesteffen commented 2 years ago

It does not throw any errors, it looks like it works fine. But when I run the createReceiptShipment and check the shipments that are returned as a response, tracking_code and carrier_name aren't set for them (I also checked the Etsy UI to see that the tracking code was not added). Only after applying the changes mentioned above it works for me

Granga commented 2 years ago

You are right. I just checked, the tracking number was not recorded on Etsy. I've applied a fix that utilizes the stringify method from query-string. It pretty much does what you suggested, with addition that it will work for all requests of this type.

"etsy-ts": "3.5.5" contains the fix.

lassesteffen commented 2 years ago

Awesome, thanks for your help!