GeezerVison is showing github.com — Breaking API change 03/2024 - 404 Error · Issue #19 · foo-git/rewe-discounts
Breaking API change 03/2024 - 404 Error #19DescriptionThe new API introduced in v2.6 (#17, #18) seems to be broken due to a change by REWE, it now yields a 404 error:
Traceback (most recent call last):
File "[...]/rewe_discounts.py", line 294, in less_elegant_query
data = scraper.get(url).json()
^^^^^^^^^^^^^^^^^^^^^^^
File "[...]/venv/lib/python3.11/site-packages/requests/models.py", line 975, in json
raise RequestsJSONDecodeError(e.msg, e.doc, e.pos)
requests.exceptions.JSONDecodeError: Extra data: line 1 column 5 (char 4)
FAIL: Unknown error while fetching discounts from https://www.rewe.de/api/all-stationary-offers/[...], maybe a typo or the server rejected the request.
Right now, I have no solution available and the script will not work. If you find the correct API url, please let me know. Activityknautschka commented on Mar 30, 2024 Hi,
with the linked discussion thread I found out what still works:
You can build the link for the API with the market-id from local REWE stores.
So for example https://shop.rewe.de/api/products/?search=&market=1766005 should work and list all products of the specific market in a JSON. (Of course you could add a search term as an addtion in the URL if you'd like).
Also, a note: I'm not sure if they changed the market-ids in general. When I used your script I got a market-id back with even the right adress of the market etc., so I thought it was right, but it didn't work.
So instead I went to the REWE website, set a local market there manually over the UI and the URL looked like this: https://www.rewe.de/marktseite/dortmund/1766005/rewe-dortmund-husenerstr-50/
So I tried what I thought should be the id (1766005) in the URL and it actually worked. The JSON you get lists also the prices for the products, which was the feature I was looking for.
Don't know if this will help you and if this is the solution you had in mind. But anyway I wanted to try to because your work here helped me anyway! Never had thought you could use the API of REWE just like that. Keep up the good work! :) foo-git commented on Mar 30, 2024 If I interpret the first link of yours correctly, it shows all products from the Rewe Lieferdienst, which is unfortunately not a list of current discounts. My Rewe store for "testing the script" yields a NO_HIT response, so the URL is not globally valid.
Regarding the market-ids, I could not reproduce your finding, as running the script with the PLZ from your store yields the correct market-id. Did you use the correct PLZ?
./rewe_discounts.py --list-markets 44319
ID Location
1766005: Filips Einzelhandels KG, Husenerstr. 50, 44319 Dortmund
562336: Carsten Engel e.K., Wickeder Hellweg 100 - 104, 44319 Dortmund
320195: Filips Supermarkt GmbH & Co. KG, Asselner Hellweg 94, 44319 Dortmund
So thanks for your much appreciated feedback, but it's not the solution yet :( knautschka commented on Mar 30, 2024 About the market-ids: Sorry, you're right! I actually had a typo and didn't notice... :D
You're right, unfortunely my link doesn't show the current discounts. I'll keep trying to find a way to get them! :) knautschka commented on Mar 30, 2024 I might have found something that could lead to a solution:
I looked manually over the UI and noticed that when you click on a product that is in discount (for example an item on https://www.rewe.de/angebote/dortmund/1766005/rewe-dortmund-husenerstr-50/?week=current), there is a request for a JSON for the product you clicked on that can be requestet with for example https://www.rewe.de/api/offer-details/15962993?wwIdent=1766005. The ending of the URL seems to be made of the product-id and the market-id.
This might be a good start. Now we have to find out how all discounted products can be requestet at once.
What I didn't find yet: At the overview of the discounted products I don't find such a request for the data although it kind of has to be there. So I don't know how to make the request yet.
I will try it further but maybe someone else will be faster than me with the API-request I posted.
Edit: Noticed that you mentioned the URL-scheme in the other issue topic. But maybe it's at least good to know that it still works. modelD-svg commented on Mar 31, 2024 I've been using the API you call "less elegant" for some time now and found this issue today, looking into why it stopped working. From what I can tell, all APIs (both browser and mobile) are now using fully cloudflare'd, WAF'd and fingerprinted endpoints. If you're curious, the app uses mobile-clients-api.rewe.de/api/stationary-app-offers/<mid> with seemingly heavy fingerprinting of course.
I've mostly given up on fighting cloudflare for projects that "just need to run", so I've gone back to just getting the raw html and parsing it with soup. This requires a vm and a sketchy ahk script, but I already have those things anyways and that has been working mostly well for similar projects for eons. Reacted by donbilbo4488 and Fabian Seitz foo-git commented on Apr 1, 2024 Maybe the VM way is the way to go. Incidentally, for v1.0 I used selenium and soup to get and process the raw html. From v2.0 onwards I switched to the APIs.
I'll check if the selenium approach still works (although it seriously inflates the dependencies). I can't give an estimation on the timeline, as I'm busy with other tasks at the moment. modelD-svg commented on Apr 1, 2024 I suspect you will have issues with cloudflare using selenium aswell, but good luck nontheless. huskycgn commented on Apr 6, 2024 I've built something similar a few months ago - unfortuntately also stopped working. :( BabyIsh88 commented on May 25, 2024 torbenpfohl commented on Jun 15, 2024 I took a look at the rewe app which uses these two endpoints:
https://mobile-api.rewe.de/api/v3/market/search?search="zipcode" for getting market ids in the area.
https://mobile-clients-api.rewe.de/api/stationary-app-offers/"market-id" for getting all offers for a market.
You have to use the same headers that the app uses and you have to specify a certificate and private key (both are in the rewe.apk).
With that I got a working version again.
But since I'm not sure if you are allowed to distribute the certificate and private key I haven't made a pull request yet. (Maybe somebody has some insight into that issue.) For now I just added a description on how to get the private key and the certificate. (in my fork) Reacted by modelD-svg and Marius Reacted by reeno and foo-git modelD-svg commented on Jun 16, 2024
I took a look at the rewe app which uses these two endpoints:
https://mobile-api.rewe.de/api/v3/market/search?search="zipcode" for getting market ids in the area. https://mobile-clients-api.rewe.de/api/stationary-app-offers/"market-id" for getting all offers for a market.
You have to use the same headers that the app uses and you have to specify a certificate and private key (both are in the rewe.apk). With that I got a working version again.
But since I'm not sure if you are allowed to distribute the certificate and private key I haven't made a pull request yet. (Maybe somebody has some insight into that issue.) For now I just added a description on how to get the private key and the certificate. (in my fork)
Very nice! May I ask how you debugged the issue? ByteSizedMarius commented on Jun 16, 2024 Have confirmed that calling the api works using the certs. I started automating the process (both extracting the certificates but also extracting the password) described by @torbenpfohl in powershell as a learning excercise (currently trying to get a bit better at ps). It's currently quite messy but if anyones interested in beta-testing it, please let me know. Otherwise I will probably publish it at some point in june. Personally, I would advise against publishing the certificates torbenpfohl commented on Jun 16, 2024 @modelD-svg
Was a bit messy. Basically I decompiled the apk with jadx and looked around in the code (with jadx-gui). Searching for "Request", "GET" and so on. Renaming a lot of functions for clarity.
At some point I learned about frida and started to hook some basic networking classes - like java.net.Socket and the Conscrypt functions - first to look at the arguments being passed and the return value, and than to look at the stacktrace.
The stacktrace limited the class I had to look at further. And from those classes it became clearer and clearer what class prepared the GET-request.
At last I hooked (with frida) a function in that class that was being called a lot and logged the class-parameters (url and headers).
The certificate and private key I found while looking through the resources of the decompiled apk (decompiled with apktool); there I found the mtls_prod.pfx file which was password protected. But searching for mtls_prod in the source code gave only a few classes and in one of them was the password (as a integer-array).
But all in all I took a lot of time and poking around in the source code + hooking a lot of functions. (was my first reverse engineering project though)
@ByteSizedMarius
Thank you for the assessment and for testing!
I mostly use a Linux distro without Powershell. But I look forward to your script. Maybe I can adapt it for bash. Reacted by Marius and Peer Bit-Barron commented on Aug 29, 2024
Can you show what the curl request (with --verbose added) on your VPS prints out?
do you know how to fix it? torbenpfohl commented on Aug 29, 2024 Bit-Barron commented on Aug 29, 2024
Not sure, yet. nginx related among other things, ipv4 related This is what I found so far, so first thing you might try is using IPv4 (--ipv4). Maybe if that's not working try using http1.1 (--http1.1).
okay i will try that, thank you Bit-Barron commented on Aug 29, 2024
Not sure, yet. nginx related among other things, ipv4 related This is what I found so far, so first thing you might try is using IPv4 (--ipv4). Maybe if that's not working try using http1.1 (--http1.1).
but if its not working, are there other methods where i can host my py backend to use it? eikaramba commented on Aug 29, 2024 update: ok i saw now that you are already able to fetch the API just not on the VPS, in that case ignore my comment
not sure if it helps but i am able to fetch the products (in my forked heissePreise repo) via:
// reweClient.js
const https = require("https");
const fs = require("fs");
const crypto = require("crypto");
const zlib = require("zlib");
const FULL_CERT_FILE_PATH = "private.pem";
const FULL_KEY_FILE_PATH = "private.key";
const client_cert = fs.readFileSync(FULL_CERT_FILE_PATH);
const client_key = fs.readFileSync(FULL_KEY_FILE_PATH);
class ReweClient {
constructor(marketId) {
this.hostname = "mobile-clients-api.rewe.de";
this.marketId = marketId;
}
async fetch(url) {
// const url = `/api/stationary-app-offers/${marketId}${path}`;
const options = this._getRequestOptions(url);
return new Promise((resolve, reject) => {
const req = https.request(options, (res) => {
let data = [];
res.on("data", (chunk) => {
data.push(chunk);
});
res.on("end", () => {
const buffer = Buffer.concat(data);
this._processResponse(buffer, res.headers, resolve, reject);
});
});
req.on("error", (error) => {
reject(error);
});
req.end();
});
}
_getRequestOptions(url) {
return {
hostname: this.hostname,
port: 443,
path: url,
method: "GET",
cert: client_cert,
key: client_key,
headers: {
"user-agent": "REWE-Mobile-Client/3.17.1.32270 Android/11 Phone/Google_sdk_gphone_x86_64",
rdfa: crypto.randomUUID(),
"Correlation-Id": crypto.randomUUID(),
"rd-service-types": "PICKUP",
"x-rd-service-types": "PICKUP",
"rd-is-lsfk": "false",
"rd-customer-zip": "48149",
"rd-postcode": "48149",
"x-rd-customer-zip": "",
"rd-market-id": this.marketId,
"x-rd-market-id": "",
"a-b-test-groups": "productlist-citrusad",
Host: this.hostname,
Connection: "Keep-Alive",
"Accept-Encoding": "gzip",
},
};
}
_processResponse(buffer, headers, resolve, reject) {
if (headers["content-encoding"] === "gzip") {
zlib.gunzip(buffer, (err, decoded) => {
if (err) {
reject(new Error("Error decompressing data: " + err.message));
return;
}
this._parseAndResolve(decoded, resolve, reject);
});
} else {
this._parseAndResolve(buffer, resolve, reject);
}
}
_parseAndResolve(data, resolve, reject) {
try {
const jsonData = JSON.parse(data.toString());
resolve(jsonData);
} catch (e) {
reject(new Error("Error parsing JSON: " + e.message));
}
}
}
module.exports = ReweClient;
and then
const client = new ReweClient(1940221);
const firstPage = await client.fetch(`/api/products?query=*&page=${pageId++}&objectsPerPage=250`);
Bit-Barron commented on Aug 29, 2024
not sure if it helps but i am able to fetch the products (in my forked heissePreise repo) via:
// reweClient.js
const https = require("https");
const fs = require("fs");
const crypto = require("crypto");
const zlib = require("zlib");
const FULL_CERT_FILE_PATH = "private.pem";
const FULL_KEY_FILE_PATH = "private.key";
const client_cert = fs.readFileSync(FULL_CERT_FILE_PATH);
const client_key = fs.readFileSync(FULL_KEY_FILE_PATH);
class ReweClient {
constructor(marketId) {
this.hostname = "mobile-clients-api.rewe.de";
this.marketId = marketId;
}
async fetch(url) {
// const url = `/api/stationary-app-offers/${marketId}${path}`;
const options = this._getRequestOptions(url);
return new Promise((resolve, reject) => {
const req = https.request(options, (res) => {
let data = [];
res.on("data", (chunk) => {
data.push(chunk);
});
res.on("end", () => {
const buffer = Buffer.concat(data);
this._processResponse(buffer, res.headers, resolve, reject);
});
});
req.on("error", (error) => {
reject(error);
});
req.end();
});
}
_getRequestOptions(url) {
return {
hostname: this.hostname,
port: 443,
path: url,
method: "GET",
cert: client_cert,
key: client_key,
headers: {
"user-agent": "REWE-Mobile-Client/3.17.1.32270 Android/11 Phone/Google_sdk_gphone_x86_64",
rdfa: crypto.randomUUID(),
"Correlation-Id": crypto.randomUUID(),
"rd-service-types": "PICKUP",
"x-rd-service-types": "PICKUP",
"rd-is-lsfk": "false",
"rd-customer-zip": "48149",
"rd-postcode": "48149",
"x-rd-customer-zip": "",
"rd-market-id": this.marketId,
"x-rd-market-id": "",
"a-b-test-groups": "productlist-citrusad",
Host: this.hostname,
Connection: "Keep-Alive",
"Accept-Encoding": "gzip",
},
};
}
_processResponse(buffer, headers, resolve, reject) {
if (headers["content-encoding"] === "gzip") {
zlib.gunzip(buffer, (err, decoded) => {
if (err) {
reject(new Error("Error decompressing data: " + err.message));
return;
}
this._parseAndResolve(decoded, resolve, reject);
});
} else {
this._parseAndResolve(buffer, resolve, reject);
}
}
_parseAndResolve(data, resolve, reject) {
try {
const jsonData = JSON.parse(data.toString());
resolve(jsonData);
} catch (e) {
reject(new Error("Error parsing JSON: " + e.message));
}
}
}
module.exports = ReweClient;
and then const firstPage = await client.fetch(/api/products?query=*&page=${pageId++}&objectsPerPage=250);
is the script hostet somewhere? eikaramba commented on Aug 29, 2024 Yes it is hosted in a docker container on my local server fetching prices every day from my home I. but not on a cloud server environment if that is the question. ByteSizedMarius commented on Aug 29, 2024
like exactly that code is working locally and on my vps not
maybe check if the vps's IP is on a blacklist. https://mxtoolbox.com/blacklists.aspx
if it's not the certificates (make sure its not the certificates somehow!) no idea torbenpfohl commented on Aug 29, 2024
Not sure, yet. nginx related among other things, ipv4 related This is what I found so far, so first thing you might try is using IPv4 (--ipv4). Maybe if that's not working try using http1.1 (--http1.1).
but if its not working, are there other methods where i can host my py backend to use it?
Homeserver (e.g. raspberrypi) might be the way to go.. but honestly I have no experience.
Adding to @ByteSizedMarius maybe check the route as well (traceroute mobile-clients-api.rewe.de). And try using a VPN (e.g. protonvpn has a free tier). angelognazzo commented on Jan 22, 2025 eikaramba commented on Jan 22, 2025 yes it seems they changed the authentication and the key is not there anymore. at least i didn't find it.
update: ahh i also downloaded the wrong apk, see answer below :P ByteSizedMarius commented on Jan 22, 2025 Starting with Rewe v4, they changed to an xapk, which is differently packaged. Torbens guide is a little more difficult than it has to be, you can just unzip it.
- download rewe-4-0-x.xapk
- rename the file to rewe.zip
- open the zip and copy de.rewe.app.mobile.apk from the zip
- rename de.rewe.app.mobile.apk to de.rewe.app.mobile.zip
- open the zip. the certificate is in /res/raw
Please note that when trying to download the apk from uptodown, you don't accidentally download the uptodown store-installer (which is the default download). They seem to have added a dark pattern in order to get you to install their store.
Download the real apk by clicking on "all variants", then the version name. The apk should be around ~75mb, not 20mb.
Please let me know if that works for you @angelognazzo
I will update my repo later today too to reflect these changes angelognazzo commented on Jan 22, 2025
Starting with Rewe v4, they changed to an xapk, which is differently packaged. Torbens guide is a little more difficult than it has to be, you can just unzip it.
1. download rewe-4-0-x.xapk
2. rename the file to rewe.zip
3. open the zip and copy de.rewe.app.mobile.apk from the zip
4. rename de.rewe.app.mobile.apk to de.rewe.app.mobile.zip
5. open the zip. the certificate is in /res/raw
Please note that when trying to download the apk from uptodown, you don't accidentally download the uptodown store-installer (which is the default download). They seem to have added a dark pattern in order to get you to install their store.
Download the real apk by clicking on "all variants", then the version name. The apk should be around ~75mb, not 20mb.
Please let me know if that works for you @angelognazzo
I will update my repo later today too to reflect these changes
Hi Marius, thanks so much, this works and helped a lot, I was not aware that I was downloading the wrong apk file from uptodown, now I managed to find the correct version and the certificate. Thanks!
And very nice work with your Go repo, I will use the opportunity to study a bit more Go! ByteSizedMarius commented on Jan 22, 2025 updated the repo, thanks for flagging. wouldn't have noticed any time soon :) ByteSizedMarius commented on Feb 7, 2026
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|