Hosted Targets Request Body Error

Hello,

I created a hosted target following the tutorial. Using the Apigee Tutorial here(https://docs.apigee.com/api-platform/hosted-targets/hosted-targets-tutorials).There was a section on creating a NodeJs/Express App on your machine and then uploading/deploying to Apigee Edge. I have that tutorial working perfectly well. All the get requests work fine. I can can access the req.params.

I also created a simple get request. This code works on my local machine. It will simply return the request body. The following code builds fine on Apigee.

app.get("/test", (req,res)=> {
res.send(req.body);
})

When I try to make a get request with a response body I get this error. If I do not have a response body, then I get the expected {}. I did this on postman, and curl I am getting the same results. When I try to trace it, the request is not captured and nothing is displayed. Things to note is that I have body-parser installed as a dependency and is on the package.json file.

Error:

<!DOCTYPE html>
<html lang=en>
    <meta charset=utf-8>
    <meta name=viewport content="initial-scale=1, minimum-scale=1, width=device-width">
    <title>Error 400 (Bad Request)!!1</title>
    <style>
    *{margin:0;padding:0}html,code{font:15px/22px arial,sans-serif}html{background:#fff;color:#222;padding:15px}body{margin:7% auto 0;max-width:390px;min-height:180px;padding:30px 0 15px}* > body{background:url(//www.google.com/images/errors/robot.png) 100% 5px no-repeat;padding-right:205px}p{margin:11px 0 22px;overflow:hidden}ins{color:#777;text-decoration:none}a img{border:0}@media screen and (max-width:772px){body{background:none;margin-top:0;max-width:none;padding-right:0}}#logo{background:url(//www.google.com/images/branding/googlelogo/1x/googlelogo_color_150x54dp.png) no-repeat;margin-left:-5px}@media only screen and (min-resolution:192dpi){#logo{background:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) no-repeat 0% 0%/100% 100%;-moz-border-image:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) 0}}@media only screen and (-webkit-min-device-pixel-ratio:2){#logo{background:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) no-repeat;-webkit-background-size:100% 100%}}#logo{display:inline-block;height:54px;width:150px}
  </style>
    <a href=//www.google.com/>
    <span id=logo aria-label=Google></span>
</a>
<p>
    <b>400.</b>
    <ins>That’s an error.</ins>
    <p>Your client has issued a malformed or illegal request.  
        <ins>That’s all we know.</ins>

Full Code :

app.yaml

runtime: node
runtimeVersion: 8
application: my-express-app
env:
  - name: NODE_ENV
    value: production
  - name: LOG_LEVEL
    value: 3

index.js

var express = require('express')
var app = express()
let bodyParser = require('body-parser');


app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());

app.get('/', function(req, res) {
    res.json({
        hello: "Hello World!"
    })
})

app.get('/hello/:name', function(req, res) {
    var name = req.params.name
    res.json({
        hello: "hello " + name
    })
})

//The only added code(get Request)
app.get("/test", (req,res)=> {
    console.log(req.body)
    res.send(req.body);
})

var server = app.listen(process.env.PORT || 9000, function() {
    console.log('Listening on port %d', server.address().port)
})

package.json

{
  "name": "hello-world",
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {
    "start": "node index.js --use_strict"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "body-parser": "^1.19.0",
    "express": "^4.16.0"
  },
  "devDependencies": {},
  "description": ""
}

TLDR: If I make an http request with a request body, I get the the error above.

Solved Solved
0 5 892
1 ACCEPTED SOLUTION

Can you provide more details please? The error you're getting is not a Hosted Target error and seeing more code would help us diagnose the issue.

View solution in original post

5 REPLIES 5

Can you provide more details please? The error you're getting is not a Hosted Target error and seeing more code would help us diagnose the issue.

Hi Jeremy, I displayed all the nodejs code above. I used almost the exact same code. It is pretty much just if I make an API request with a request body, I get an error. Even without the added code, I still get an error. For example, if I make a request to the route ('/') with a request body, I will get the same error mentioned above. I have not added any policies, it is purely a nodejs error imo.

Hey Dom,

It looks like the error you're getting is a 400 from Google AppEngine, the backend for Hosted Targets. The root issue is that you are sending a request body on a HTTP GET.

There is a known bug in AppEngine where sending a request body on GET results in a 400. https://issuetracker.google.com/36886282

Thanks for discovering this bug. We'll make sure to update our docs to cover this!

Regards,
Josh

What happens when you run the same code locally and you make the same requests? I would expect it to behave the same, as in Express is returning a "400" for your request. Can you show your cURL invocation? If you are indeed providing a body in your request, your client might be defaulting to a POST request because a GET request with a body is not typical, and many libraries/tools don't support it because it's not advised. (The HTTP spec doesn't prohibit it but it's not common practice to provide a body for a GET request.) It might also help to add some simple logging for your requests to log what request is making it to Express.

Hi Jeremy,

It works now. I changed it from a get to a post. It is printing out the data.Thanks for the help and informing on the best practice.