简体   繁体   中英

URLFetch Service always sending null on POST request

I am having a bit of trouble with URLFetch service that it is always sending a json String as null:

            url = new URL(URL);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setDoOutput(true);
            conn.setRequestMethod("POST");
            conn.setConnectTimeout(GAE_PUMP_TIME);
            writer = new OutputStreamWriter(conn.getOutputStream());
            writer.write(jsonString);
            writer.close();

            if(conn.getResponseCode() == HttpURLConnection.HTTP_OK || conn.getResponseCode() == HttpURLConnection.HTTP_NO_CONTENT){
                LOG.info("Response: " + "OK");
                setStatus(Status.SUCCESS_OK);
            } else {
                LOG.info("Response: " + "ERROR");
                setStatus(Status.SERVER_ERROR_INTERNAL);
            }

The above code is always throwing server error due to the fact that when we inspected the data sent or received by the server is null.

I tried send through other REST client (like Curl) the String jsonString manually, and the server is able to process. So this concludes that the data is well-formed. The question is why GAE URLFetch service when doing POST method it cannot send the jsonString what is missing in my code?

I'm not having this problem from Python (none of my Java apps post JSON). The only thing of significance I see is that when POSTing JSON, my apps set the Content-Type header to application/json .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM