HTTP POST request gets transformed to GET request
Asked Answered
C

2

0

How exactly does the WireCloud proxy work? We use the following code for a request via the WireCloud proxy:

MashupPlatform.http.makeRequest(url, {
        method: 'POST',
        forceProxy: true,
        onSuccess: function (response) {
            success(response);
        },
        onFailure: function (response) {
            error(response);
        },
        onComplete: function () {
            complete();
        }
    });

The browser network analysis shows that a POST request gets send to https://example.com/cdp/https/rest.example.com/path/to/service. Our webservice that gets called by the url however logs, that it receives a GET request.

The access log of our rest service hosted by a tomcat shows:

192.168.60.221 - - [26/May/2016:10:38:31 +0200] "GET /path/to/service HTTP/1.1" 405 1013
192.168.60.221 - - [26/May/2016:10:38:42 +0200] "POST /path/to/service HTTP/1.1" 204 -

The first call is done with the MashupPlatform.http.makeRequest call listed above, the second call is done with jQuery like this:

$.ajax({
        type: "POST",
        url: url,
        data: null,
        success: success
    });

This works perfeclty fine when we set the CORS header in our webservice.

So what may be the reason, that WireClouds proxy does not work as expected?

Cataclysm answered 23/5, 2016 at 8:55 Comment(4)
Our webservice that gets called by the url however logs what does it log? and where?Duckling
The tomcat loggs the requested url and the request method in its access log.Cataclysm
@Newbird, it's better to provide a snippet with the logs (you can remove any private data previously). If you don't provide that data, we are unable to review it for detecting the problem (See the stackoverflow guidelines for more details). Another source of info that can be useful in this case is the request info provided by the developers tools of the browser.Perfumer
The logs are added to the post now. I thought they might not be that interesting since you can only read from it, if a post or a get request got received by the tomcat. The request info from the browser was already stated in my question: 'The browser network analysis shows that a POST request gets send.' I also added the called URL with the post request.Cataclysm
B
1

There are a lot of widgets and operators using the MashupPlatform API and the WireCloud's CORS proxy and we have never see it transforming POST requests into GET requests. Anyway, it's fine to use jQuery ;-). Moreover, you can also use it for performing requests to services not supporting the CORS headers by using the buildProxyURL method. E.g:

url = MashupPlatform.http.buildProxyURL('https://api.example.com/api/endpoing');
$.ajax({
    method: "POST",
    url: url,
    data: null,
    success: success
});
Bewilder answered 23/5, 2016 at 10:49 Comment(0)
M
0

Although @Alvaro's answer is the right one, mine might help other users that encountered a similar issue WordPress: a POST request was transformed in a GET requests on some servers. It turns out that I needed to add the following header that mimics what WordPress usually accepts as POST requests.

method: "POST",
headers: { 'content-type': 'application/x-www-form-urlencoded; charset=UTF-8' },
...

Other ways to make it work for a XMLHTTPRequest object named oReq:

oReq.setRequestHeader("content-type", "application/x-www-form-urlencoded; charset=UTF-8");

And in PHP that helped too:

curl_setopt(CURLOPT_HTTPHEADER, array(
    "content-type: application/x-www-form-urlencoded; charset=UTF-8"));

Why?

Debugging my requests and comparing them to the requests WordPress receives when editing regular posts indicated that my requests were automatically given the header "content-type: text/plain; charset=UTF-8", which caused trouble.

Mettle answered 17/1, 2023 at 23:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.