1

i want to build an API as a serverless AWS Lambda Function and use ClaudiaJS as a framework. However, when passing a JSON object to the POST route, i cannot parse the contents of request.body correctly since they are of the type "string" instead of type "object". If this were an express node.js backend, i would just use bodyParser, but in this case i cannot. Any help appreciated :)

I tried JSON.parse(req.body), but to no avail.

This is the code for the POST route

var ApiBuilder = require('claudia-api-builder'),
api = new ApiBuilder();

module.exports = api;

api.post('/upload', (req, res) => {
  return req.body;           //I return the body for debugging purposes 
});

When posting the JSON Object to the service using POSTMAN (content-type:application/json)

{
  "latitude": "52.514818",
  "longitude": "13.356101",
  "additionalData": "xyc"
}

it returns a string instead of an object. I therefore cannot parse it like: req.body.latitude and get the value for the latitude.

"----------------------------641080260577727375179249\r\nContent-Disposition: form-data; name=\"file\"; filename=\"Berlin.json\"\r\nContent-Type: application/json\r\n\r\n{\n  \"latitude\": \"52.514818\",\n  \"longitude\": \"13.356101\",\n  \"additionalData\": \"xyc\"\n}\n\r\n----------------------------641080260577727375179249--\r\n"
malte238749874
  • 159
  • 2
  • 12
  • My goal is to parse it like: req.body.latitude and get the value for the latitude. – malte238749874 Aug 02 '19 at 13:49
  • In the example above i use ```return req.body;``` – malte238749874 Aug 02 '19 at 13:52
  • The whole req (stands for request) object looks like this: ``` { "v": 3, "rawBody": "----------------------------053307385004798184943745\r\nContent-Disposition: form-data; name=\"file\"; filename=\"Berlin.json\"\r\nContent-Type: application/json\r\n\r\n{\n \"latitude\": \"52.514818\",\n \"longitude\": \"13.356101\",\n \"additionalData\": \"xyc\"\n}\n\r\n----------------------------053307385004798184943745--\r\n", [... //rest too long for posting here] } ``` – malte238749874 Aug 02 '19 at 13:58
  • If I assume correct `req.body` is the request-body you send to the server, that one is a string. If you are looking for the response you will probably find it in `res.response`. – Lain Aug 02 '19 at 13:58
  • I am not sure about this Claudia thingy, but in Express.js. for example. you have to set a body-parser, otherwise you get form-data. Isn't this the case for Claudia as well? – Thales Minussi Aug 02 '19 at 13:59
  • @ThalesMinussi: The official docs at https://github.com/claudiajs/claudia-api-builder/blob/master/docs/request-object.md describe that it is not needed: body: in case of an application/json, the body of the request, parsed as a JSON object; in case of application/xml or text/plain POST or PUT, the body of the request as a string. In case of binary content, a Buffer. – malte238749874 Aug 02 '19 at 14:01
  • Thanks, @maltewirz. I am also checking the docs at the moment. In the meantime, can you please `console.log(req)`? – Thales Minussi Aug 02 '19 at 14:04
  • @Lain: I do not intend to send a response, this is only used for debugging purposes since it runs on a aws lambda function. I return req.body because I want to see if it extracts the contents of the JSON that i posted to it. Once this is successful, i will store the contents of req.body in a DB. – malte238749874 Aug 02 '19 at 14:05
  • @ThalesMinussi: I console logged it on the server side: `{ v: 3, rawBody: '----------------------------937710845832309840347183\r\nContent-Disposition: form-data; name="file"; filename="Berlin.json"\r\nContent-Type: application/json\r\n\r\n{\n "latitude": "52.514818",\n "longitude": "13.356101",\n "additionalData": "xyc"\n}\n\r\n----------------------------937710845832309840347183--\r\n', normalizedHeaders:` So it's basically the same issue ^^ – malte238749874 Aug 02 '19 at 14:14
  • OK, looks like you are uploading a file but also sending things on the body. You can't do both simultaneously. – Thales Minussi Aug 02 '19 at 14:19
  • @maltewirz: I understand that. But as sad as it sounds the body is neither an object and consists of more information than just your passed object. Do you want to turn the whole request-body into an object? Just check if the `req` object offers you other properties which already provide what you seek. – Lain Aug 02 '19 at 14:22
  • Hm, what do you mean? I only post a JSON file to the API endpoint using Postman. Where do i send information on the body? – malte238749874 Aug 02 '19 at 14:24
  • OK i logged the following: `console.log("req.body.latitude", req.body.latitude);` this return `undefined` ! and when we look at: `console.log("req.body", req.body);` this returns the string `Content-Disposition: form-data; name=""; filename="Berlin.json" Content-Type: application/json { "latitude": "52.514818", "longitude": "13.356101", "additionalData": "xyc" } ----------------------------576780127607820385409727--` – malte238749874 Aug 02 '19 at 14:30
  • so the problem is that `req.body` sends much more info than just the object... – malte238749874 Aug 02 '19 at 14:34

1 Answers1

0

The issue you have, is that you are sending your API form data and expecting it to behave like JSON.

The easiest solution would be to send the actual JSON in the POST body, in which case your existing code will work.

Otherwise you will just have to grab the JSON from the existing string.

var ApiBuilder = require('claudia-api-builder'), api = new ApiBuilder();
module.exports = api;

api.post('/upload', (req, res) => {
  console.log(req.body);  // outputs the form-data as string
  var myString = req.body.substring(
    req.body.lastIndexOf("{"), 
    req.body.lastIndexOf("}")+1
  );
  var myJson = JSON.parse(myString);
  console.log(myJson) // outputs a valid JSON object
  return myObj;
});
K Mo
  • 2,125
  • 8
  • 16